#include<iostream>//2368987 2010-04-21 23:57:41 Accepted 1242 78MS 340K 1316 B C++ 悔惜晟
using namespace std;
char map[205][205];
int dir[4][2] = { {0, 1}, {0, -1}, {1, 0}, {-1, 0} };
int n, m;
int si, sj;
bool flag;
int t;
int ri, rj;
void dfs(int x, int y, int count)
{
int i;
int p, q;
//if(x < 0 || y < 0 || x >= n || y >= m)
//return;
if(x == si && y == sj)
{
if(count < t)
t = count;
flag = true;
//t = count;
return ;
}
for(i = 0; i < 4; i++)
{
p = x + dir[i][0];
q = y + dir[i][1] ;
if(map[p][q] != '#' && p >= 0 && p < n && q >= 0 && q < m)
{
if(map[p ][ q] == 'x')
{
map[p ][q] = '#';
dfs(p, q, count + 2);
map[ p ][ q ] = 'x';
}
//if(map[ p ][ q ] == 'x')
else
{
map[ p ][ q ] = '#';
dfs(p, q, count + 1);
map[ p ][ q ] = '.';
}
}
}
//return ;
}
int main()
{
while(cin>>n>>m)
{
int i, j;
//int ri, rj;
for(i = 0; i < n; i++)
for(j = 0; j < m; j++)
{
cin>>map[i][j];
if(map[i][j] == 'a')
{
ri = i;
rj = j;
}
if(map[i][j] == 'r')
{
si = i;
sj = j;
}
}
flag = false;
t = 100000000;
map[ri][rj] = '#';
dfs(ri, rj, 0);
if( flag )
cout<<t<<endl;
else
cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
}
}
#include<iostream> //为何这个不可以????测试数据都过不了?why??
using namespace std;
char map[20][20];
int dir[4][2] = { {0, 1}, {0, -1}, {1, 0}, {-1, 0} };
int n, m;
int si, sj;
bool flag;
int t;
void dfs(int x, int y, int count)
{
int i;
if(x < 0 || y < 0 || x >= n || y >= m)
return;
if(x == si && y == sj)
{
flag = true;
t = count;
return ;
}
for(i = 0; i < 4; i++)
{
if(map[ x + dir[i][0] ][ y + dir[i][1] ] == '.')
{
map[ x + dir[i][0] ][ y + dir[i][1] ] = '#';
dfs(x + dir[i][0], y + dir[i][1], count + 1);
map[ x + dir[i][0] ][ y + dir[i][1] ] = '.';
}
if(map[ x + dir[i][0] ][ y + dir[i][1] ] == 'x')
{
map[ x + dir[i][0] ][ y + dir[i][1] ] = '#';
dfs(x + dir[i][0], y + dir[i][1], count + 2);
map[ x + dir[i][0] ][ y + dir[i][1] ] = 'x';
}
}
return ;
}
int main()
{
while(cin>>n>>m)
{
int i, j;
int ri, rj;
for(i = 0; i < n; i++)
for(j = 0; j < m; j++)
{
cin>>map[i][j];
if(map[i][j] == 'a')
{
ri = i;
rj = j;
}
if(map[i][j] == 'r')
{
si = i;
sj = j;
}
}
flag = false;
map[ri][rj] = '#';
dfs(ri, rj, 0);
if( flag )
cout<<t<<endl;
else
cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
}
}