Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are Wils, ROADs, and GUARDs in the prison.
Angel's friends want to save Angel. their task is: approach Angel. we assume that "approach Angel" is to get to the position where Angel stays. when there's a guard in the grid, we must kill him (or her ?) To move into the grid. we assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. and we are strong enough to kill all the guards.
You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course .)
Input
First line contains two integers stand for N and M.
Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.
Process to the end of the file.
Output
For each test case, your program shocould output a single integer, standing for the minimal time needed. if such a number does no exist, you showould output a line containing "Poor ANGEL has to stay in the prison all his life."
Sample Input
7 8
#.#####.
#. A #... r.
#... # X...
..#..#.#
#...##..
.#......
........
Sample Output
13
The breadth first traversal solution. The time of each step is uncertain, unlike the ordinary labyrinth Shortest Path solution. Therefore, you must use an additional data storage time and traverse all reachable data.
So Will BFS be infinitely searched? No, because we have added the judgment condition that this walk method takes less time than before.
[Cpp]
# Include <iostream>
# Include <queue>
# Include <stdio. h>
Using namespace std;
# Define maxmn200
# Define INF 1000000
Class Point {
Public:
Int x, y, time;
};
Queue <Point> Q;
Int N, M;
Char map [MAXMN] [MAXMN];
Int mintime [MAXMN] [MAXMN]; // indicates the shortest time required to reach the current position.
Int dir [4] [2] = {-1, 0}, {0,-1}, {1, 0}, {0, 1 }};
Int ax, ay; // end position
Int bfs (Point s) {// search from Point s
Q. push (s );
Point hd;
While (! Q. empty ()){
Hd = Q. front ();
Q. pop ();
For (int I = 0; I <4; I ++ ){
Int x = hd. x + dir [I] [0];
Int y = hd. y + dir [I] [1];
If (x> = 0 & x <N & y> = 0 & y <M & map [x] [y]! = '#'){
Point t;
T. x = x;
T. y = y;
T. time = hd. time + 1;
If (map [x] [y] = 'X') t. time ++;
If (t. time <mintime [x] [y]) {
Mintime [x] [y] = t. time;
Q. push (t );
}
}
}
}
Return mintime [ax] [ay];
}
Int main (){
While (scanf ("% d", & N, & M )! = EOF ){
For (int I = 0; I <N; I ++)
Scanf ("% s", map [I]);
Int sx, sy;
Point start;
For (int I = 0; I <N; I ++ ){
For (int j = 0; j <M; j ++ ){
Mintime [I] [j] = INF; // The value of mintime is the maximum at the beginning and cannot be reached.
If (map [I] [j] = 'A '){
Ax = I;
Ay = j;
} Else if (map [I] [j] = 'R '){
Sx = I;
Sy = j;
}
}
}
Start. x = sx;
Start. y = sy;
Start. time = 0;
Mintime [sx] [sy] = 0;
Int mint = bfs (start );
If (mint <INF)
Printf ("% d \ n", mint );
Else
Printf ("Poor ANGEL has to stay in the prison all his life. \ n ");
}
Return 0;
}
# Include <iostream>
# Include <queue>
# Include <stdio. h>
Using namespace std;
# Define maxmn200
# Define INF 1000000
Class Point {
Public:
Int x, y, time;
};
Queue <Point> Q;
Int N, M;
Char map [MAXMN] [MAXMN];
Int mintime [MAXMN] [MAXMN]; // indicates the shortest time required to reach the current position.
Int dir [4] [2] = {-1, 0}, {0,-1}, {1, 0}, {0, 1 }};
Int ax, ay; // end position
Int bfs (Point s) {// search from Point s
Q. push (s );
Point hd;
While (! Q. empty ()){
Hd = Q. front ();
Q. pop ();
For (int I = 0; I <4; I ++ ){
Int x = hd. x + dir [I] [0];
Int y = hd. y + dir [I] [1];
If (x> = 0 & x <N & y> = 0 & y <M & map [x] [y]! = '#'){
Point t;
T. x = x;
T. y = y;
T. time = hd. time + 1;
If (map [x] [y] = 'X') t. time ++;
If (t. time <mintime [x] [y]) {
Mintime [x] [y] = t. time;
Q. push (t );
}
}
}
}
Return mintime [ax] [ay];
}
Int main (){
While (scanf ("% d", & N, & M )! = EOF ){
For (int I = 0; I <N; I ++)
Scanf ("% s", map [I]);
Int sx, sy;
Point start;
For (int I = 0; I <N; I ++ ){
For (int j = 0; j <M; j ++ ){
Mintime [I] [j] = INF; // The value of mintime is the maximum at the beginning and cannot be reached.
If (map [I] [j] = 'A '){
Ax = I;
Ay = j;
} Else if (map [I] [j] = 'R '){
Sx = I;
Sy = j;
}
}
}
Start. x = sx;
Start. y = sy;
Start. time = 0;
Mintime [sx] [sy] = 0;
Int mint = bfs (start );
If (mint <INF)
Printf ("% d \ n", mint );
Else
Printf ("Poor ANGEL has to stay in the prison all his life. \ n ");
}
Return 0;
}