从天使的位置开始,直到找到r,优先队列每次选择最短时间的路
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<sstream>
#include<queue>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int M = 1010;
struct node
{
int x, y, step;
bool operator <(const node &b)const
{
return step > b.step;
}
};
int n, m;
priority_queue<node>q;
char mp[210][210];
int sx, sy;
bool vis[210][210];
int dir[][2] = { 1,0,-1,0,0,1,0,-1 };
int bfs()
{
node st;
st.x = sx;
st.y = sy;
st.step = 0;
vis[sx][sy] = 1;
q.push(st);
while (!q.empty())
{
node nw = q.top();
q.pop();
if (mp[nw.x][nw.y] == 'r')
{
return nw.step;
}
for (int i = 0; i < 4; ++i)
{
int xx = nw.x + dir[i][0];
int yy = nw.y + dir[i][1];
if(xx<0 ||xx>=n ||yy<0||yy>=m ||vis[xx][yy])
continue;
if(mp[xx][yy]=='#')
continue;
if (mp[xx][yy] == 'x')
{
vis[xx][yy] = 1;
node nx;
nx.x = xx;
nx.y = yy;
nx.step = nw.step + 2;
q.push(nx);
}
else
{
vis[xx][yy] = 1;
node nx;
nx.x = xx;
nx.y = yy;
nx.step = nw.step + 1;
q.push(nx);
}
}
}
return -1;
}
int main()
{
while (~scanf("%d%d", &n, &m))
{
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
cin >> mp[i][j];
if (mp[i][j] == 'a')
{
sx = i;
sy = j;
}
}
}
memset(vis, 0, sizeof(vis));
int flag=bfs();
if (flag == -1)
{
puts("Poor ANGEL has to stay in the prison all his life.");
}
else
{
printf("%d\n", flag);
}
}
return 0;
}