#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
char map[202][202];
const int maxi = 0x7FFFFFFF;int n, m,Count;
struct site {
int l, r,time;
friend bool operator<(site c1, site c2)
{
return c2.time < c1.time;
}
};
int Move[4][2] = { {-1,0},{1,0},{0,-1},{0,1} },mintime[202][202];
bool in(int a, int b){
return a > 0 && a <= n&& b > 0 && b <= m&&map[a][b]!='#'; }//判断点是否在范围内,同时不是墙壁
int main() {
bool judge;
site b,c;
while (cin >> n >> m) {
judge=0;
memset(mintime, 0, sizeof(mintime));
for (int i =1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> map[i][j];
if (map[i][j] == 'a') { b.l = i; b.r = j; }//终点b
else if (map[i][j] == 'r') {
c.l = i;
c.r = j;
c.time = 0;
}//起点c
}//处理输入信息
}
priority_queue<site>a;
site cur, next;
a.push(c);
while (!a.empty()) {
cur = a.top();
a.pop();
if (cur.l == b.l && cur.r == b.r) {
judge = 1;
Count = cur.time;
}
for (int i = 0; i < 4; i++) {//移动的四个方向
next.l = cur.l + Move[i][0];
next.r = cur.r + Move[i][1];
if (in(next.l, next.r)) {//判断能否到达
next.time = cur.time + 1;//无论如何都+1
if (map[next.l][next.r] == 'x') next.time++;//如果是守卫再加1
if (next.time <= mintime[next.l][next.r] || mintime[next.l][next.r] == 0) {
mintime[next.l][next.r] = next.time;
a.push(next);
}
}
}
}
if (judge)cout <<Count<< endl;
else cout<<"Poor ANGEL has to stay in the prison all his life." << endl;
}
}
做到了后面的题目才发现,好像可以直接标记点(x,y)有没有被访问过,毕竟是用了优先队列,先出来(判断能否满足条件的走法)肯定是时间在队列中是最小的走法。