RescueTime limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 16314 accepted submission (s): 5926
Problem descriptionangel 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 .)
Inputfirst 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.
Outputfor 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
Authorchen, Xue
Sourcezoj monthly, October 2003.
#include <stdio.h>#include <string.h>#include <queue>#define maxn 202using std::priority_queue;int n, m, ans;const int mov[][2] = {0, 1, 0, -1, -1, 0, 1, 0};char map[maxn][maxn];struct Node{int x, y, time;friend bool operator<(Node a, Node b){return a.time > b.time;}};bool check(int x, int y){return x >= 0 && x < n && y >= 0 && y < m && map[x][y] != '#';}bool BFS(int x, int y){Node now, tmp;now.x = x; now.y = y;map[x][y] = '#';now.time = 0;priority_queue<Node> Q;Q.push(now);while(!Q.empty()){now = Q.top(); Q.pop();for(int i = 0; i < 4; ++i){tmp = now;tmp.x += mov[i][0];tmp.y += mov[i][1];if(check(tmp.x, tmp.y)){++tmp.time;if(map[tmp.x][tmp.y] == 'x')++tmp.time;else if(map[tmp.x][tmp.y] == 'r'){ans = tmp.time; return true;}map[tmp.x][tmp.y] = '#';Q.push(tmp);}}}return false;}int main(){int i, j, x, y;while(scanf("%d%d", &n, &m) == 2){for(i = 0; i < n; ++i){getchar();for(j = 0; j < m; ++j){map[i][j] = getchar();if(map[i][j] == 'a'){x = i; y = j;}}}if(BFS(x, y)) printf("%d\n", ans);else printf("Poor ANGEL has to stay in the prison all his life.\n");}return 0;}