#include "iostream" #include "queue" #define M 1000000 using namespace std; typedef struct{ int x; int y; int step; }Position; Position temp,t,start[201],end; int delta[4][2]={{-1,0},{0,1},{1,0},{0,-1}}; char map[201][201]; int col,row,Min; bool visit[201][201],visit_f[201][201]; void Bfs() { queue<Position> q; visit[end.x][end.y]=true; end.step=0; q.push(end); while(!q.empty()) { temp=q.front(); q.pop(); // cout<<temp.x<<" "<<temp.y<<" "<<temp.step<<endl; if(visit_f[temp.x][temp.y]==true) { if(temp.step<Min) Min=temp.step; } if(map[temp.x][temp.y]=='x') { temp.step++; map[temp.x][temp.y]='.'; if(Min>temp.step) q.push(temp); //printf("/n"); } else { for(int i=0;i<4;i++) { t.x=delta[i][0]+temp.x; t.y=delta[i][1]+temp.y; if(!visit[t.x][t.y]&&(map[t.x][t.y]!='#')&&t.x>0&&t.x<=col&&t.y>0&&t.y<=row) { t.step=temp.step+1; visit[t.x][t.y]=true; if(Min>t.step) q.push(t); } } } } } int main() { freopen("data.in","r",stdin); while(scanf("%d%d",&col,&row)!=EOF) { Min=M; memset(visit,false,sizeof(visit)); memset(visit_f,false,sizeof(visit_f)); for(int i=1;i<=col;i++) { for(int j=1;j<=row;j++) { cin>>map[i][j]; if(map[i][j]=='r') visit_f[i][j]=true; if(map[i][j]=='a') { end.x=i; end.y=j; } } } Bfs(); if(Min!=M) cout<<Min<<endl; else cout<<"Poor ANGEL has to stay in the prison all his life."<<endl; } while(1); return 0; }