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)
If there is a pit at (0, 0), the agent dies immediately. There will not be a Wumpus at (0, 0).
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.)
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".
2 3 1 1 1 2 2 0 3 2 2 -1 -1 -1 3 1 1 1 3 2 2 -1 -1 -1
850 870
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:
题意:
给你一个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; }