//http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <map>
#define maxn 200 + 10
#define INF 0x7fffffff
using namespace std;
char g[maxn][maxn];
int stx, sty;
bool vis[maxn][maxn];
int move[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
int n, m;
struct POINT
{
int cur;
int x, y;
};
priority_queue<POINT> q;
bool operator<(POINT a, POINT b)
{
return a.cur > b.cur;
}
int bfs()
{
POINT tmp;
memset(vis, false, sizeof(vis));
tmp.x = stx;
tmp.y = sty;
tmp.cur = 0;
vis[stx][sty] = true;
q.push(tmp);
while(!q.empty())
{
tmp = q.top();
q.pop();
for(int i = 0; i < 4; ++i)
{
int dcur = tmp.cur;
int dx = tmp.x + move[i][0];
int dy = tmp.y + move[i][1];
if(dx >= 0 && dx < n && dy >= 0 && dy < m && g[dx][dy] != '#' && !vis[dx][dy])
{
vis[dx][dy] = true;
if(g[dx][dy] == 'a')
return dcur+1;
else if(g[dx][dy] == '.')
{
POINT t;
t.cur = dcur+1;
t.x = dx;
t.y = dy;
q.push(t);
}
else if(g[dx][dy] == 'x')
{
POINT t;
t.cur = dcur+2;
t.x = dx;
t.y = dy;
q.push(t);
}
}
}
}
return -1;
}
int main()
{
while(scanf("%d%d", &n, &m) != EOF)
{
while(!q.empty())
q.pop();
memset(vis, false, sizeof(vis));
for(int i = 0; i < n; ++i)
{
scanf("%s", g[i]);
for(int j = 0; j < m; ++j)
if(g[i][j] == 'r')
stx = i, sty = j;
}
int ans = bfs();
if(ans == -1) puts("Poor ANGEL has to stay in the prison all his life.");
else printf("%d\n", ans);
}
return 0;
}
ZOJ 1649 Rescue
最新推荐文章于 2019-07-07 04:49:19 发布