HDOJ1242. Rescue(BFS+priority_queue)


Rescue
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 34431    Accepted Submission(s): 12019
Problem Description
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+priority_queue(优先队列)

        题意:Angel被传说中神秘的邪恶的Moligpy人抓住了!他被关在一个迷宫中。迷宫的长、宽不超过200。

                   迷宫中有不可以越过的墙以及监狱的看守。Angel的朋友带了一些救援队来到了迷宫中。他们的任务是:接近Angel。

                   我们假设接近Angel就是到达Angel所在的位置。假设移动需要1单位时间,杀死一个看守也需要1单位时间。到达一个格子以后,如果该格子有看守,则一定要杀死。

                   问:最少要多少单位时间,才能到达Angel所在的地方?(只能向上、下、左、右4个方向移动)

        注意以下三个要点:

        (1)‘a’--Angel  'r'--Angel的朋友  ‘x’--看守  ‘.’--空地(可走区域)  ‘#’--障碍(不可走区域);

        (2)"Angel's friends"提示我们Angel('a')只有一个,而她的朋友('r')可以有多个,因此在搜索时逆向处理一下,从天使向周围搜索,找"离她最近"的朋友;

        (3)移动需要1单位时间,杀死一个看守也需要1单位时间。也就是说,搜索时遇到'.'时间+1,而遇到'x'时间+2,因此可使用优先队列(priority_queue)实现该过程。

#include 
#include 
#include 
#include 
using namespace std;
const int MAX=210;
int N,M;
int sx,sy,mint;    //(sx,sy)-     mint-       
char map[MAX][MAX];//     
int vis[MAX][MAX]; //       
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};  //    ( / / / ) 
struct node
{
	int x;
	int y;
	int t;
	//  'b.t);
	}
} cur,nxt;
//      (x,y)      、        
bool check(int x,int y)
{
	return (x>=0 && x=0 && y q;
	//           
	cur.x=sx;
	cur.y=sy;
	cur.t=0;
	//      
	q.push(cur);
	//     (      ,       ) 
	vis[cur.x][cur.y]=1;
	while(!q.empty())
	{
		cur=q.top();
		q.pop();
		//  Angel   ,        (         Angel   ) 
		if(map[cur.x][cur.y]=='r')
			return cur.t;
		//      (cur.x,cur.y)   4    
		for(i=0;i<4;i++)
		{
			nxt.x=cur.x+dir[i][0];
			nxt.y=cur.y+dir[i][1];
			//       
			if(check(nxt.x,nxt.y))
			{
				//       ,  +2 
				if(map[nxt.x][nxt.y]=='x')
					nxt.t=cur.t+2;
				//    +1 
				else
					nxt.t=cur.t+1;
				//      (nxt.x,nxt.y)    
				vis[nxt.x][nxt.y]=1;
				//     (nxt.x,nxt.y)   
				q.push(nxt);
			}
		}
	}
	return -1;
}
int main()
{
	int i,j;
	while(scanf("%d %d",&N,&M)!=EOF)
	{
		for(i=0;i