# Include <iostream>
# Include <queue>
Using namespace STD;
Char A [205] [205], guodu [205] [205];
Int T [205] [205], guodu2 [205] [205];
Int map [4] [2] = {}, {}, {0,-1 }};
Int X, Y, counter;
Struct Node
{
Int X;
Int y;
};
Node duostart [40000], start, temp, end;
Int BFS (queue <struct node> Qu, int Han, int lie)
{
Int I, V;
While (! Qu. Empty ())
{
Temp = Qu. Front ();
Qu. Pop ();
for (I = 0; I <4; I ++)
{< br> X = temp. X + map [I] [0];
Y = temp. Y + map [I] [1];
If (x> = 0 & x = 0 & Y {< br> Start. X = x;
Start. y = y;
V = T [temp. y] [temp. x] + 1;
if (a [y] [x] = 'X')
V ++;
If (v {< br> Qu. push (start);
T [start. y] [start. x] = V;
}
}
}
}
If (T [end. Y] [end. x]! = 9999)
Return T [end. Y] [end. x];
Else
Return 0;
}
Int main ()
{
Int Han, lie, I, J, K, counter, Min, jilu;
While (CIN> Han> lie )! = NULL)
{
Counter = 0;
Min = 9999;
For (I = 0; I <Han; I ++)
For (j = 0; j <lie; j ++)
T [I] [J] = 9999;
Queue <struct node> qu;
For (I = 0; I <Han; I ++)
For (j = 0; j <lie; j ++)
{
Cin> A [I] [J];
If (A [I] [J] = 'R ')
{
Duostart [Counter]. x = J;
Duostart [Counter]. Y = I;
A [I] [J] = '#';
T [I] [J] = 0;
Counter ++;
}
If (A [I] [J] = 'A ')
{
End. x = J;
End. Y = I;
A [I] [J] = ';';
}
If (A [I] [J] = '.')
A [I] [J] = ';';
}
For (I = 0; I <Han; I ++)
For (j = 0; j <lie; j ++)
Guodu [I] [J] = A [I] [J];
For (I = 0; I <Han; I ++)
For (j = 0; j <lie; j ++)
Guodu2 [I] [J] = T [I] [J];
For (I = 0; I <counter; I ++)
{
Qu. Push (duostart [I]);
Jilu = BFS (qu, Han, lie );
If (min> jilu & jilu! = 0)
{
Min = jilu;
}
For (k = 0; k <Han; k ++)
For (j = 0; j <lie; j ++)
A [k] [J] = guodu [k] [J];
For (k = 0; k <Han; k ++)
For (j = 0; j <lie; j ++)
T [k] [J] = guodu2 [k] [J];
}
If (Min! = 9999)
Cout <min <Endl;
Else
Cout <"Poor Angel has to stay in the prison all his life." <Endl;
}
Return 0;
}