hdoj 1242 Rescue

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19985    Accepted Submission(s): 7110


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
 此题使用优先队列和普通队列都可以(将优先队列对应代码已经注释,)
两者不同的地方已经用上下空行隔开

 

#include<stdio.h>
#include<string.h>
#include<queue>
#define MAX 210
using namespace std;
char map[MAX][MAX];
int n,m;
struct node
{
	int x;
	int y;
	int time;

	/*friend bool operator < (node a,node b)//定义结构体的优先队列 
	{
		return a.time>b.time;//花费时间少的先出队 
	}*/    //普通队列不需要这几行

};
void bfs(int x1,int y1,int x2,int y2)
{
	int j,i,ok=0;

	//priority_queue<node>q;//定义结构 体优先队列  
    queue<node>q;//此为普通队列

	int move[4][2]={0,1,0,-1,1,0,-1,0};
	node begin,end;
	begin.x=x1;
	begin.y=y1;
	begin.time=0;
	q.push(begin);
	while(!q.empty())
	{

		//end=q.top();//优先队列
        end=q.front;  //普通队列  与优先队列的一个区别就是此处,弹出队首时所用函数不一样

		q.pop();
		if(end.x==x2&&end.y==y2)
		{
			ok=1;
			break;
		}
		for(i=0;i<4;i++)
		{
			begin.x=end.x+move[i][0];
			begin.y=end.y+move[i][1];
			if(0<=begin.x&&begin.x<n&&0<=begin.y&&begin.y<m&&map[begin.x][begin.y]!='#')
			{					
				if(map[begin.x][begin.y]=='x')
				begin.time=end.time+2;	
				else 
				begin.time=end.time+1;
				map[begin.x][begin.y]='#'; 
				q.push(begin);
				
			}
		}
	}
	if(ok)
	printf("%d\n",end.time);//注意这里
	else
	printf("Poor ANGEL has to stay in the prison all his life.\n");
}
int main()
{
	int j,i,s,t,k,x1,x2,y1,y2;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		for(i=0;i<n;i++)
		scanf("%s",map[i]);
		for(i=0;i<n;i++)
		{
			for(j=0;j<m;j++)
			{
				if(map[i][j]=='a')
				{
					x2=i;y2=j;
				}
				else if(map[i][j]=='r')
				{
					x1=i;y1=j;
				}
			}
		}
		bfs(x1,y1,x2,y2);
	}
	return 0;
}

 

你可能感兴趣的:(res)