ZOJ 3890 Wumpus
Wumpus
64-bit integer IO format: %lld Java class name: Main
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:
Source
Author
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 25; 4 int d[maxn][maxn][4][2],mp[maxn][maxn],n; 5 struct node { 6 int x,y,z,t; 7 node(int a = 0,int b = 0,int c = 0,int d = 0) { 8 x = a; 9 y = b; 10 z = c; 11 t = d; 12 } 13 }; 14 queue<node>q; 15 bool isIn(int x,int y) { 16 if(x < n && y < n && x > -1 && y > -1) 17 return mp[x][y] == 0 || mp[x][y] == 3; 18 return false; 19 } 20 int bfs() { 21 while(!q.empty()) q.pop(); 22 static const int dir[4][2] = {0,-1,-1,0,0,1,1,0}; 23 q.push(node(0,0,3,0)); 24 d[0][0][3][0] = 0; 25 while(!q.empty()) { 26 node now = q.front(); 27 q.pop(); 28 int fx = (now.z + 1)%4; 29 if(d[now.x][now.y][fx][now.t] > d[now.x][now.y][now.z][now.t] + 1) { 30 d[now.x][now.y][fx][now.t] = d[now.x][now.y][now.z][now.t] + 1; 31 q.push(node(now.x,now.y,fx,now.t)); 32 } 33 fx = (now.z + 3)%4; 34 if(d[now.x][now.y][fx][now.t] > d[now.x][now.y][now.z][now.t] + 1) { 35 d[now.x][now.y][fx][now.t] = d[now.x][now.y][now.z][now.t] + 1; 36 q.push(node(now.x,now.y,fx,now.t)); 37 } 38 int nx = now.x + dir[now.z][0]; 39 int ny = now.y + dir[now.z][1]; 40 if(!isIn(nx,ny)) continue; 41 if(mp[nx][ny] == 3 && !now.t) { 42 if(d[nx][ny][now.z][1] > d[now.x][now.y][now.z][0] + 1) { 43 d[nx][ny][now.z][1] = d[now.x][now.y][now.z][0] + 1; 44 q.push(node(nx,ny,now.z,1)); 45 } 46 } 47 if(d[nx][ny][now.z][now.t] > d[now.x][now.y][now.z][now.t] + 1) { 48 d[nx][ny][now.z][now.t] = d[now.x][now.y][now.z][now.t] + 1; 49 q.push(node(nx,ny,now.z,now.t)); 50 } 51 } 52 int ret = d[0][0][0][1]; 53 for(int i = 1; i < 4; ++i) ret = min(ret,d[0][0][i][1]); 54 ret = 980 - ret*10; 55 return ret < 0?-1:ret; 56 } 57 int main() { 58 int kase,t,x,y; 59 scanf("%d",&kase); 60 while(kase--) { 61 scanf("%d",&n); 62 memset(d,0x3f,sizeof d); 63 memset(mp,0,sizeof mp); 64 while(~scanf("%d%d%d",&t,&x,&y)) { 65 if(t == -1 && x == -1 && y == -1) break; 66 mp[x][y] = t; 67 } 68 printf("%d\n",bfs()); 69 } 70 return 0; 71 }