1 #include<iostream>
2 #include<queue>
3 #include<cstdio>
4 using namespace std;
5
6 char mp[201][201];
7 int m,n,dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
8
9 struct point
10 {
11 int x,y,step;
12 friend bool operator <(point t1,point t2)
13 {
14 return t1.step>t2.step;
15 }
16 };
17
18 void bfs(point S)
19 {
20 int i;
21 point t,tt;
22 priority_queue<point>Q;
23 Q.push(S);
24 while(!Q.empty())
25 {
26 t=Q.top();
27 Q.pop();
28 for(i=0;i<4;i++)
29 {
30 tt.x=t.x+dir[i][0];tt.y=t.y+dir[i][1]; tt.step=t.step+1;
31 if(tt.x<1||tt.x>m||tt.y<1||tt.y>n||mp[tt.x][tt.y]=='#') continue;
32 if(mp[tt.x][tt.y]=='r') {printf("%d\n",tt.step);return;}
33 if(mp[tt.x][tt.y]=='x') tt.step++;
34
35 mp[tt.x][tt.y]='#';
36 Q.push(tt);
37 }
38 }
39 printf("Poor ANGEL has to stay in the prison all his life.\n");
40 return;
41 }
42
43 int main()
44 {
45 int i,j;
46 point S;
47
48 while(scanf("%d%d",&m,&n)!=EOF)
49 {
50 getchar();
51 for(i=1;i<=m;i++)
52 {
53 for(j=1;j<=n;j++)
54 {
55 scanf("%c",&mp[i][j]);
56 if(mp[i][j]=='a')
57 {
58 S.x=i;
59 S.y=j;
60 S.step=0;
61 }
62 }
63 getchar();
64 }
65 bfs(S);
66 }
67 return 0;
68 }