zoj1649

maksyuki 发表于 oj 分类,标签:
0

Rescue

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 WALLs, 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 should output a single integer, standing for the minimal time needed. If such a number does no exist, you should 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

 

题目类型:”有权值”的BFS

算法分析:本题使用一个mintime[x][y]二维数组来表示起点到图中(x,y)位置处的最短时间,由于最短路径中的某一个方案所得出的结果不一定最优解,所以一定要将所有的方案都搜到之后,才能输出结果。对于每一个节点,只扩展到下一个节点位置(相邻的4个方向)处所用的时间比mintime在该位置所用时间少的。BFS完成之后直接输出mintime[end_x][end_y]即可