Rescue
Process to the end of the file.
附上AC代码:
#include <iostream> #include <cstdio> #include <string> #include <cmath> #include <iomanip> #include <ctime> #include <climits> #include <cstdlib> #include <cstring> #include <algorithm> #include <queue> #include <vector> #include <set> #include <map> using namespace std; typedef unsigned int UI; typedef long long LL; typedef unsigned long long ULL; typedef long double LD; const double pi = acos(-1.0); const double e = exp(1.0); const short MAX = 205; const short dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; short n, m; char mat[MAX][MAX]; bool flag[MAX][MAX]; struct point { short x, y; int step; bool operator < (const point & p) const{ return step > p.step; } }; int bfs(int x, int y); int main() { ios::sync_with_stdio(false); while (cin >> n >> m) { for (int i=0; i<n; i++) cin >> mat[i]; int x, y; memset(flag, 0, sizeof(flag)); for (int i=0; i<n; i++) for (int j=0; j<m; j++) if (mat[i][j] == 'a') { x = i; y = j; break; } flag[x][y] = 1; int ans = bfs(x, y); if (ans) cout << ans << endl; else cout << "Poor ANGEL has to stay in the prison all his life.\n"; } return 0; } int bfs(int x, int y) { priority_queue<point> que; point p; p.x = x; p.y = y; p.step = 0; que.push(p); while (!que.empty()) { p = que.top(); que.pop(); x = p.x; y = p.y; int step = p.step; if (mat[x][y] == 'r') return step; for (int i=0; i<4; ++i) { p.x = x+dir[i][0]; p.y = y+dir[i][1]; p.step = 0; if (p.x>=0 && p.x<n && p.y>=0 && p.y<m) { if (mat[p.x][p.y]=='.' || mat[p.x][p.y]=='r') p.step = step+1; else if (mat[p.x][p.y]=='x') p.step = step+2; if (p.step>0 && flag[p.x][p.y]==0) { flag[p.x][p.y] = 1; que.push(p); } } } } return 0; }