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)
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:
题目大意:
从(0,0)去取金子,其中有些点不能走,每步只能前走,左转,右转,捡金子或者出来,求最少的步数能够取完金子并出来。
解题思路:
记忆化搜索,vis[a][b][c][d],a代表是否取金子,a,b表示当前的位置,d代表的方向,还有就是左转和右转,这里有个小技巧,将方向转化
为0~3的数字,这样就可以+1右转,-1左转。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
struct node
{
int x,y,cur,step,dir;
};
bool vis[2][25][25][4];
queue<node> q;
int a[25][25];
int n,enx,eny;
int d[4][2]= {{0,1},{1,0},{0,-1},{-1,0}};
int ans;
void BFS()
{
node p;
while(!q.empty())
{
q.pop();
}
p.x=0,p.y=0,p.cur=0,p.step=0,p.dir=1;
vis[0][0][0][1]=true;
q.push(p);
while(!q.empty())
{
p=q.front();
q.pop();
if(p.step>100)
{
ans=-1;
break;
}
if(p.cur==1&&p.x==0&&p.y==0)
{
ans=1000-(p.step+1)*10;
if(ans<0)
ans=-1;
break;
}
if(p.cur==0&&p.x==enx&&p.y==eny)
{
p.step+=1;
p.cur=1;
q.push(p);
vis[1][p.x][p.y][p.dir]=true;
continue;
}
node p2;
p2.dir=p.dir;
p2.x=p.x+d[p.dir][0];
p2.y=p.y+d[p.dir][1];
p2.step=p.step+1,p2.cur=p.cur;
if(p2.x>=0&&p2.x<n&&p2.y>=0&&p2.y<n&&(!a[p2.x][p2.y]))
{
if(!vis[p2.cur][p2.x][p2.y][p2.dir])
{
q.push(p2);
vis[p2.cur][p2.x][p2.y][p2.dir]=true;
}
}
p2.dir=(p.dir+1)%4;
p2.x=p.x,p2.y=p.y;
p2.step=p.step+1,p2.cur=p.cur;
if(!vis[p2.cur][p2.x][p2.y][p2.dir])
{
q.push(p2);
vis[p2.cur][p2.x][p2.y][p2.dir]=true;
}
p2.dir=(p.dir-1+4)%4;
p2.x=p.x,p2.y=p.y;
p2.step=p.step+1,p2.cur=p.cur;
if(!vis[p2.cur][p2.x][p2.y][p2.dir])
{
q.push(p2);
vis[p2.cur][p2.x][p2.y][p2.dir]=true;
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(a,0,sizeof(a));
memset(vis,0,sizeof(vis));
int cur,x,y;
while(~scanf("%d%d%d",&cur,&x,&y)&&cur!=-1)
{
if(cur==1||cur==2)
a[x][y]=1;
else
{
enx=x;
eny=y;
}
}
ans=-1;
BFS();
printf("%d\n",ans);
}
return 0;
}