HDU 1242 Rescue

来源:互联网 发布:开源房产cms 编辑:程序博客网 时间:2024/06/05 02:45
#include<stdio.h>#include<string.h>#include<queue>using namespace std;const int maxn = 1e3 + 5;const int INF = 0x3f3f3f3f;char mp[maxn][maxn];int vis[maxn][maxn];int dx[10] = {0,0,-1,1,1,1,-1,-1};int dy[10] = {1,-1,0,0,-1,1,-1,1};int n,m;struct node{int x,y;int step;node(int _x, int _y, int _step = 0): x(_x), y(_y),step(_step){}bool operator < (const node & i)const{return step > i.step;}};int main (){while (scanf("%d%d", &n, &m) != EOF){for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){mp[i][j] = INF;}}for (int i = 0; i < n; i++){scanf("%s",mp[i]);}int sx,sy;for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){if (mp[i][j] == 'a'){sx = i;sy = j;}}}priority_queue<node> que;que.push(node(sx,sy));memset(vis,0,sizeof(vis));vis[sx][sy] = 1;int ans = 0;while (!que.empty() ){node tem = que.top() ;que.pop() ;if (mp[tem.x ][tem.y ] == 'r'){ans = tem.step ;break;}for (int i = 0; i < 4; i++){int x = tem.x + dx[i];int y = tem.y + dy[i];if (x < 0 || y < 0 || x >= n || y >= m) continue;else if (vis[x][y])continue;else if (mp[x][y] == '.' || mp[x][y] == 'r') { // 开始忘了在这判断r,结果总是出错que.push(node(x,y,tem.step + 1)); vis[x][y] = 1;continue;}else if (mp[x][y] == '#')continue; //#不允许通过,题上好像没说else  {que.push(node(x,y,tem.step + 2)); vis[x][y] = 1;}}}if (!que.empty() ){printf("%d\n",ans);}else printf("Poor ANGEL has to stay in the prison all his life.\n");}}