hdu 1242 Rescue(bfs+优先队列)

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+优先队列…
bfs能找到最优解的原因是…时间更短的点一定是先访问到先入队…
但是由于守卫的存在…先访问到的节点如果是守卫的话就不是时间更短的节点…
所以这个时候我们要用优先队列…
第一次写.
然后学习了重载<的写法...写在结构体里面和外面的..
感觉q老师…窝只是请教他这个重载在写结构体里面要怎样…他就把kuangbin的模板发了我一份
学到了不少.
还有就是,因为朋友有多个,而小天使只有一个…
不如让小天使去找朋友…逆向思维…
一遍ac,开心.

View Code

 

 
 
Posted in ACM

说点什么

您将是第一位评论人!

提醒
wpDiscuz