链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3890
Wumpus Time Limit: 2 Seconds Memory Limit: 65536 KBOne 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
题意:
有一个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 */