zoj 3890Wumpus

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)

zoj 3890Wumpus_第1张图片
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

Hint

For the sample 1, the following steps are taken:
turn left, forward, forward, turn right, forward, forward, grab, turn left, turn left, forward, forward, turn left, forward, forward, climb.
There are in all 15 steps, so the final score is 840.For the sample 2 , the path is as follow:

zoj 3890Wumpus_第2张图片



题意:

  给你一个n*n的洞穴,里面有怪兽,陷阱,钱。你从(0,0)出发,你能前进,左钻,右转,那金子,爬出洞穴,每一步都会消耗10块钱,遇到陷阱或怪兽就死了,拿到黄金就得到1000块,问你最后出来如果钱是负的或中途死了就输出-1,否则输出剩余的钱。


思路:

记忆化bfs,用f[x][y][f][g]来记录该状态,x,y是位置,f是方向,g用来表示有没有拿到黄金。然后就bfs搜索就行了。

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

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <queue>
#include <iostream>
using namespace std;
int res[22][22];
int n;
struct node{
	int x,y,d,f,g;	
};
queue<node>q;
int inmap(int x,int y)
{
	if(x<0||x>=n||y<0||y>=n||res[x][y]==1||res[x][y]==2)
		return 0;
	return 1;
}
int min(int a,int b)
{
	return a<b?a:b;
}
int f[22][22][4][2];
int fang[4][2]={-1,0,0,1,1,0,0,-1};
int bfs()
{
	int i;
	while(!q.empty())
		q.pop();
	node a;
	a.x=a.y=a.d=a.g=0;
	a.f=2;
	q.push(a);
	int ans=1000;
	while(!q.empty())
	{
		node temp=q.front();
		q.pop();
		if(temp.x==0&&temp.y==0&&temp.g==1)
		{
			temp.d++;
			ans=min(ans,temp.d);
		}
		for(i=0;i<4;i++)
		{
			node next;
			int xx=temp.x+fang[i][0];
			int yy=temp.y+fang[i][1];
			if(inmap(xx,yy))
			{
				if(abs(i-temp.f)==1||abs(i-temp.f)==3)
				{
					next.d=temp.d+2;
					next.f=i;
				}
				else if(abs(i-temp.f)==2)
				{
					next.d=temp.d+3;
					next.f=i;
				}
				else
				{
					next.d=temp.d+1;
					next.f=temp.f;
				}
				next.x=xx;
				next.y=yy;
				next.g=temp.g;
				if(res[xx][yy]==3)
				{
					next.g=1;
					next.d++;
				}
				if(f[xx][yy][next.f][next.g]==0)                        //刚开始做没用标记,直接用了优先队列,结果超了内存。
				{
					f[xx][yy][next.f][next.g]=next.d;
					q.push(next);
				}
				else if(next.d<f[xx][yy][next.f][next.g])
				{
					f[xx][yy][next.f][next.g]=next.d;
					q.push(next);
				}
			}	

		}
	}
	return ans;	
}
int main()
{
	int t,i,j,x,y,z;
	scanf("%d",&t);	
	while(t--)
	{
		scanf("%d",&n);
		memset(res,0,sizeof(res));
		memset(f,0,sizeof(f));
		int temp=0;
		while(scanf("%d%d%d",&z,&x,&y)!=-1)
		{
			if(z==-1&&x==-1&&y==-1)
				break;
			res[x][y]=z;
			if(x==0&&y==0&&z==2)
				temp=1;
		}
		if(temp)            //如果起点就是陷阱直接输出-1
		{
			printf("-1\n");
			continue;
		}
		int ans=bfs();
		int t=1000-10*ans;
		if(t>=0)
			printf("%d\n",t);
		else
			printf("-1\n");
		
	}
	return 0;
}


你可能感兴趣的:(zoj 3890Wumpus)