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
直接bfs,注意建图的方向
#include<cstdio> #include<cmath> #include<queue> #include<vector> #include<stack> #include<map> #include<string> #include<cstring> #include<algorithm> #include<iostream> using namespace std; typedef long long ll; const ll maxn = 25; int T, n, flag, x, y; int mp[maxn][maxn]; int f[maxn][maxn][4][2]; struct point { int x, y, d, v; point(int x, int y, int d, int v) :x(x), y(y), d(d), v(v){} }; int bfs() { queue<point> p; p.push(point(0, 0, 0, 0)); if (mp[0][0] == 1) return -1; f[0][0][0][0] = 0; int x, y, d, v; while (!p.empty()) { point q = p.front(); p.pop(); if (f[q.x][q.y][q.d][q.v] > 98) continue; x = q.x; y = q.y; v = q.v; if (mp[x][y] == 3 && f[x][y][q.d][1] > f[x][y][q.d][0]) { f[x][y][q.d][1] = f[x][y][q.d][0]; p.push(point(x, y, q.d, 1)); } d = (q.d + 1) % 4; if (f[x][y][d][v] > f[x][y][q.d][v] + 1) { f[x][y][d][v] = f[x][y][q.d][v] + 1; p.push(point(x, y, d, v)); } d = (q.d + 3) % 4; if (f[x][y][d][v] > f[x][y][q.d][v] + 1) { f[x][y][d][v] = f[x][y][q.d][v] + 1; p.push(point(x, y, d, v)); } d = q.d; if (d == 0) ++x; if (d == 1) --y; if (d == 2) --x; if (d == 3) ++y; if (x >= 0 && x < n&&y >= 0 && y<n) if (mp[x][y] != 1 && mp[x][y] != 2) if (f[x][y][d][v] > f[q.x][q.y][d][v] + 1) { f[x][y][d][v] = f[q.x][q.y][d][v] + 1; p.push(point(x, y, d, v)); } } int ans = f[0][0][0][1]; for (int i = 1; i < 4; i++) ans = min(ans, f[0][0][i][1]); ans = 980 - ans * 10; if (ans < 0) return -1; return ans; } int main() { scanf("%d", &T); while (T--) { scanf("%d", &n); memset(mp, 0, sizeof(mp)); while (scanf("%d%d%d", &flag, &x, &y) == 3) { if (x == -1 && y == -1 && flag == -1) break; mp[x][y] = flag; } memset(f, 1, sizeof(f)); printf("%d\n", bfs()); } return 0; }