码迷,mamicode.com
首页 > 其他好文 > 详细

zoj 3890 Wumpus bfs

时间:2017-07-08 10:02:26      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:uda   int   game   ant   pre   next   cannot   gre   online   

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3890

Wumpus

Time Limit: 2 Seconds      Memory Limit: 65536 KB

One day Leon finds a very classic game called Wumpus.The game is as follow.
Once an agent fell into a cave. The legend said that in this cave lived a kind of monster called Wumpus, and there were horrible pits which could lead to death everywhere. However, there were also a huge amount of gold in the cave. The agent must be careful and sensitive so that he could grab all of the gold and climb out of the cave safely.

The cave can be regarded as a n*n board. In each square there could be a Wumpus, a pit, a brick of gold, or nothing. The agent would be at position (0,0) at first and headed right.(As the picture below)

For each step, there are six possible movements including going forward, turning left, turning right, shooting, grabbing the gold, and climbing out of the cave. If the agent steps into a square containing a pit or Wumpus, he will die. When the agent shoots, the Wumpus in front of him will die. The goal of the agent is to grab all of the gold and return to the starting position and climb out(it‘s OK if any Wumpus is still living).When a brick of gold is grabbed successfully, you will gain 1000 points. For each step you take, you will lose 10 points.

Your job is to help him compute the highest point he can possibly get.

For the purpose of simplification, we suppose that there is only one brick of gold and the agent cannot shoot the Wumpus.

If there is a pit at (0, 0), the agent dies immediately. There will not be a Wumpus at (0, 0).

Input

There are multiple cases. The first line will contain one integer k that indicates the number of cases.

For each case:
The first line will contain one integer n (n <= 20).
The following lines will contain three integers, each line shows a position of an object. The first one indicates the type of the object. 1 for Wumpus, 2 for pit and 3 for gold. Then the next two integers show the x and y coordinates of the object.
The input end with -1 -1 -1. (It is guaranteed that no two things appear in one position.)

Output

The output contains one line with one integer, which is the highest point Leon could possibly get. If he cannot finish the game with a non-negative score, print "-1".

Sample Input

2
3
1 1 1
2 2 0
3 2 2
-1 -1 -1
3
1 1 1
3 2 2
-1 -1 -1

Sample Output

850
870


题意:

有一个n*n的迷宫。然后输入 金矿 怪兽 陷阱的位置。


做法:

他能够前进。转向。挖矿。爬出出口,每一步耗费10分。挖到金矿加1000分。

以坐标,方向,有没有挖过金矿。开四位记录vis 状态。

然后打一遍 bfs即可了。

注意怪兽可能在起始点要特判,还有就是可能他没有金矿能够去挖,就输出-1而不是-10。


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <malloc.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
using namespace std;

struct point 
{
	int x,y,gg,fen,ff;
};


int vis[22][22][4][2];//四个方向 金子拿了没有

int dir[4][2]={//zai 0 0
	1,0,
	0,-1,
	-1,0,
	0,1,
};

char mp[22][22];

int n;
int kan(point &nw)
{
	if(nw.x<0||nw.x>=n||nw.y<0||nw.y>=n)
		return 0;//buneng zou
	if(mp[nw.x][nw.y]==1)
		return 0;
	if(vis[nw.x][nw.y][nw.ff][nw.gg])
		return 0;

	vis[nw.x][nw.y][nw.ff][nw.gg]=1;
	
	if(mp[nw.x][nw.y]==3&&nw.gg==0)
	{
		nw.fen+=1000;
		nw.fen-=10;
		nw.gg=1;
	}
	return 1;
}

int bfs()
{
	memset(vis,0,sizeof vis);
	point nw,sta,nxt;
	queue<point>q;
	sta.x=sta.y=0;
	sta.fen=sta.gg=0;
	sta.ff=0;
	kan(sta);
	q.push(sta);
	int ans=0;

	while(!q.empty())
	{
		nw=q.front();
		if(nw.x==0&&nw.y==0)  
			ans=max(ans,nw.fen); 
		q.pop();

		for(int i=-1;i<=1;i++)
		{
			nxt=nw;
			nxt.fen-=10;
			if(i==0)
			{
				nxt.x+=dir[nxt.ff][0];
				nxt.y+=dir[nxt.ff][1]; 
			}
			else
			{
				nxt.ff=(nxt.ff+i+4)%4;
			} 
			if(kan(nxt))
			{ 
				q.push(nxt);
			} 
		}
	} 
	return ans-10;
}





int main() 
{ 
	int tt;
	scanf("%d",&tt);
	while(tt--)
	{   
		scanf("%d",&n);
		int bi,x,y;
		int flag=1;
		memset(mp,0,sizeof mp);
		while(scanf("%d%d%d",&bi,&x,&y),x!=-1&&bi!=-1&&y!=-1)
		{
			if(bi==1) 
				mp[x][y]=1; 
			if(bi==2)
			{
				if(x==0&&y==0)
					flag=0;
				mp[x][y]=1;

			}
			if(bi==3)
				mp[x][y]=bi;
		}

		if(flag==0)//guishou -1  nabudaojinzi  -1 haishi 0
		{
			puts("-1");
			continue;
		}
		int ans=bfs();
		if(ans<0)
		printf("-1\n");
		else
			printf("%d\n",ans);
	}
	return 0;
}

 

/*
8
3 3
3 1
3 2
3 3

2 
1


8 2 1

99 5 5
50 3
50 4
59 5
60 6
60 7
50
46
51
0
49
45
7
3

70
66


99 5 5
50 3
50 4
50 5
50 6
60 7
70
*/



zoj 3890 Wumpus bfs

标签:uda   int   game   ant   pre   next   cannot   gre   online   

原文地址:http://www.cnblogs.com/ljbguanli/p/7135690.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!