杭電1242-Rescue(bfs+優先キュー|キュー)


Rescue
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21824    Accepted Submission(s): 7776
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
 

Author
CHEN, Xue
 

Source
ZOJ Monthly, October 2003
 


题意:
    Angel被传说中神秘的邪恶的Moligpy人抓住了!他被关在一个迷宫中。迷宫的长、宽不超过200。 迷宫中有不可以越过的墙以及监狱的看守。
Angel的朋友带了一些救援队来到了迷宫中。他们的任务是:接近Angel。我们假设接近Angel就是到达Angel所在的位置。

假设移动需要1单位时间,杀死一个看守也需要1单位时间。到达一个格子以后,如果该格子有看守,则一定要杀死。交给你的任务是,最少要多少单位时间,才能到达Angel所在的地方?(只能向上、下、左、右4个方向移动)


这个题是个很好的搜索题,有一个坑就是一定要从天使开始搜索盆友,因为盆友的数量不定的,我们找的是离天使最近的那个盆友,但是以前不知道的时候从天使搜索竟然没错!数据太水了!

还有一点需要注意的是遇到警卫时时间要加二,如果还是用普通队列就要注意了,不处理的话求出来的一定不是最小时间,所以第一次遇到标记一下,第二次如果碰到标记,时间再加一,不再往4面搜索!但是用优先队列就不用管那么多了,遇到警卫就加二就行了

所以这个题两种解法:

1:优先队列

2:普通队列+特殊处理

下面给出代码!


1:优先队列


#include
#include
#include
#include
using namespace std;
char map[210][210];
int vis[210][210],mov[4][2]={0,1,0,-1,1,0,-1,0};
int m,n;
struct node 
{
	int x,y,step;
	friend bool operator y.step;
	}
}now,nex;
bool can(node x)//           
{
	if(x.x<0||x.y<0||x.x>m-1||x.y>n-1||vis[x.x][x.y]||map[x.x][x.y]=='#')
	return false;
	return true;
}
int bfs(int x,int y)
{
	priority_queueq;
	now.x=x;
	now.y=y;
	now.step=0;
	q.push(now);//       
	while(!q.empty())
	{
		now=q.top();
		q.pop();
		if(map[now.x][now.y]=='r')//           ,     
		return now.step;
		for(int i=0;i<4;i++)
		{
			nex.x=now.x+mov[i][0];
			nex.y=now.y+mov[i][1];
			if(can(nex))
			{
				if(map[nex.x][nex.y]=='x')
				nex.step=now.step+2;//        ,      ,     
				else
				nex.step=now.step+1;//        ,     
				vis[nex.x][nex.y]=1;
				q.push(nex);//       
			}
		}
	}
return -1;
}

int main()
{
	int i,j,sx,sy;
	while(scanf("%d%d",&m,&n)!=EOF)
	{
		for(i=0;i

2:通常キュー+特殊処理
#include
#include
#include
#include
using namespace std;
char map[210][210];
int vis[210][210],mov[4][2]={1,0,-1,0,0,1,0,-1};
int m,n;
struct node
{
	int x,y,step,flag;
}now,nex;
bool can(node x)//           
{
	if(vis[x.x][x.y]||x.x<0||x.x>m-1||x.y<0||x.y>n-1||map[x.x][x.y]=='#')
	return false;
	return true;
}

int bfs(int a,int b)
{
	int i;
	queue q;
	now.x=a;
	now.y=b;
	now.step=0;
	now.flag=0;
	q.push(now);//       
	while(!q.empty())
	{
		now=q.front();
		q.pop();
		if(now.flag)//           ,    flag=0 
		{
			now.step++;
			now.flag=0;
			q.push(now);//             
			continue;
		}
		if(map[now.x][now.y]=='r')//           ,     
		return now.step;
		for(i=0;i<4;i++)
		{
			nex.x=now.x+mov[i][0];
			nex.y=now.y+mov[i][1];
			if(can(nex)) 
			{
				nex.step=now.step+1;//        ,     
				
				if(map[nex.x][nex.y]=='x')
				nex.flag=1;
				else
				nex.flag=0;//       ,    , flag    1 
				
				vis[nex.x][nex.y]=1;
				q.push(nex);//       
			}
		}
	}
	return -1;//        -1 
}
int main()
{
	int i,j,sx,sy;
	while(scanf("%d%d",&m,&n)!=EOF)
	{
		for(i=0;i