/* If the answer is BFs, the result does not match the meaning of the question. Then change the DFS.
# Include <iostream>
# Include <queue>
Using namespace STD;
Struct st {
Int X;
Int y;
Int step;
};
Char A [201] [201];
Int B [201] [201];
Int C [4] [2] = {}, {-}, {}, {0,-1 }};
Int Sx, Sy, ex, ey;
Int M, N;
Bool K;
Void BFS (){
// Memset (B, 0, sizeof (B ));
Queue <st> q;
St Q, S;
Q. x = SX; q. Y = sy; q. Step = 0;
B [SX] [sy] = 1;
Q. Push (Q );
While (! Q. Empty ()){
S = Q. Front ();
If (S. x = ex & S. Y = ey ){
K = 1;
Cout <S. Step <Endl;
Return;
}
Q. Pop ();
Int I;
For (I = 0; I <4; I ++ ){
Q. x = S. x + C [I] [0]; q. Y = S. Y + C [I] [1];
If (Q. x <0 | Q. Y <0 | Q. x> = M | Q. Y> = N) continue;
If (B [q. x] [q. Y] = 1) continue;
If (A [q. x] [q. Y] = '.') Q. Step = S. Step + 1;
Else if (a [q. x] [q. Y] = 'X') Q. Step = S. Step + 2;
Q. Push (Q); B [q. x] [q. Y] = 1;
}
}
Return;
}
Int main (){
Freopen ("1.txt"," r ", stdin );
Freopen ("2.txt"," W ", stdout );
While (CIN> m> N ){
Int I, j; k = 0;
For (I = 0; I <m; I ++ ){
For (j = 0; j <n; j ++ ){
Cin> A [I] [J];
If (A [I] [J] = '.' | A [I] [J] = 'X') B [I] [J] = 0;
Else if (a [I] [J] = '#') B [I] [J] = 1;
Else if (a [I] [J] = 'R') {SX = I; Sy = J ;}
Else if (a [I] [J] = 'A') {EX = I; ey = J ;}
}
}
BFS ();
If (k = 0) cout <"Poor Angel has to stay in the prison all his life./N ";
}
Return 0;
}*/
**************************************** ***********
# Include <iostream>
Using namespace STD;
Char A [201] [201];
Int C [4] [2] = {0, 1}, {0,-1 }};
Int Sx, Sy, ex, ey;
Bool L; int M, N;
Int Minn = 100000;
Void DFS (int x, int y, int t ){
Int Q, P;
// If (x <0 | Y <0 | x> = M | Y> = N) return;
If (x = ex & Y = ey ){
L = 1;
If (Minn> T) Minn = T;
Return;
}
Int I;
For (I = 0; I <4; I ++ ){
Q = x + C [I] [0]; P = Y + C [I] [1];
If (A [Q] [p]! = '#' & P> = 0 & P <n & q> = 0 & q <m ){
If (A [Q] [p] = 'X '){
A [Q] [p] = '#';
DFS (Q, P, T + 2 );
A [Q] [p] = 'X ';
}
Else {// if (a [Q] [p] = '.' | A [Q] [p] = 'R') Pay attention to the boundary !!! 2!
A [Q] [p] = '#';
DFS (Q, P, t + 1 );
A [Q] [p] = '.';
}
}
}
Return;
}
Int main (){
Freopen ("1.txt"," r ", stdin );
Freopen ("2.txt"," W ", stdout );
While (CIN> m> N ){
Int I, j; L = 0; Minn = 1000000;
For (I = 0; I <m; I ++ ){
For (j = 0; j <n; j ++ ){
Cin> A [I] [J];
If (A [I] [J] = 'A ')
{SX = I; Sy = J ;}
If (A [I] [J] = 'R ')
{EX = I; ey = J ;}
}
}
A [SX] [sy] = '#';
DFS (sx, Sy, 0 );
If (l) cout <Minn <Endl;
Else cout <"Poor Angel has to stay in the prison all his life." <Endl;
}
Return 0;
}