# Rescue BFS

Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.

Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.

You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)

Input

First line contains two integers stand for N and M.

Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.

Process to the end of the file.

Output

For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."

Sample Input

7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........

Sample Output

13

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
int n,m;
int xa,ya,xr,yr;
char map[220][220];
int vis[220][220];
int dis[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct node
{
int x,y,step;
bool operator < (const node& t)const{
return step>t.step;
}
};
int  bfs()
{
memset(vis,0,sizeof(vis));
priority_queue<node>q;
node f;
f.x=xa;
f.y=ya;
f.step=0;
vis[xa][ya]=1;
q.push(f);
while(!q.empty())
{
f=q.top();
q.pop();
if(map[f.x][f.y]=='r')
{
return f.step;
}
// 朋友不只是一个，而天使却是一个，所以我们可以反过来寻找，天使找朋友，那么目标状态就是找到朋友
for(int i=0;i<4;i++)
{
node next;
int xi=f.x+dis[i][0];
int yi=f.y+dis[i][1];
if(xi>=0 &&xi<n && yi>=0 && yi<m && map[xi][yi]!='#' && !vis[xi][yi])
{
vis[xi][yi]=1;
next.x=xi;
next.y=yi;
if(map[xi][yi]=='x')
next.step=f.step+2;
else
next.step=f.step+1;
q.push(next);
}
}
}
return -1;
}
int main()
{
while(cin>> n >> m)
{

for(int i=0;i<n;i++)
{
scanf("%s",map[i]);
for(int j=0;j<m;j++)
{
if(map[i][j]=='a')
{
xa=i;
ya=j;
}
}
}
int ans=bfs();
if(ans==-1)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",ans);
}

return 0 ;
}

#include<iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

int n,m;
char map[205][205];
int vis[205][205];
/*两个作用
1.标记作用，刚开始初始化为1，先将起始点初始化为0，以防回到起点
2.记录当前点的最短路径
*/
int x1,x2,y1,y2;
int mov[][2] = {1,0,-1,0,0,1,0,-1};

struct node
{
int x,y,step;
//友元函数 重载<
//优先队列就是node型的  在优先队列里按照step排序
friend bool operator<(node n1,node n2)
{
return n2.step<n1.step;
}
};

int place(int x,int y)
{
if(x<0 || y<0 || x>=n || y>=m || !vis[x][y] || map[x][y] == '#')
return 1;//出界或者为墙
return 0;
}

int bfs()
{
int i;
priority_queue<node> Q;//优先队列
node a,next;
a.x = x1;
a.y = y1;
a.step = 0;
Q.push(a);//进队
vis[x1][y1] = 0;//初始化
while(!Q.empty())
{
a = Q.top();//队头
Q.pop();//出队
if(a.x == x2 && a.y == y2)
return a.step;
//四个方向
for(i = 0; i<4; i++)
{
next = a;//移动后的结点
next.x+=mov[i][0];
next.y+=mov[i][1];
if(place(next.x,next.y))//判断 走不通
continue;//跳出当前循环
next.step++;//每动一次要加1
if(map[next.x][next.y] == 'x')//卫兵处多花费了一秒
next.step++;
if(vis[next.x][next.y]>=next.step)//存入最小时间
{
vis[next.x][next.y] = next.step;//第二个作用 更新最小
Q.push(next);
}//只有起点标记，其它点未标记只是最小值，还可以走
}
}
return 0;
}

int main()
{
while(cin>>n>>m)
{
for(int i = 0; i<n; i++)
{
scanf("%s",map[i]);
for(int j = 0; map[i][j]; j++)
{
if(map[i][j] == 'r')
{
x1 = i;
y1 = j;
}
else if(map[i][j] == 'a')
{
x2 = i;
y2 = j;
}
}
}
memset(vis,1,sizeof(vis));
int ans = 0;
ans = bfs();
if(ans)
printf("%d\n",ans);
else
printf("Poor ANGEL has to stay in the prison all his life.\n");
}

return 0;
}//Time Limit Exceeded