1. 程式人生 > >zoj 3890 Wumpus bfs

zoj 3890 Wumpus bfs

climb oar input data cpp eof ons starting con

鏈接: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