HDU:1242 Rescue(BFS+优先队列)

Rescue

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


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
 

Recommend
Eddy
 



题目大意:r去找a,.是路,#是墙,x是士兵,每走一个.用时为1,每遇到一个x打翻他并且过了该点用时为2,求r到a所用最短时间(注意不是最短路径,

比如r.
        xa这种情况,那么r到a最短必须选择.而不是x,由此得知该题用bfs+优先队列做)




#include <stdio.h>
#include <queue>
#include <string.h>
#include <algorithm> 
using namespace std;
int n,m;
char map[211][211];
int sx,sy,ex,ey;
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
struct node
{
	int x,y;
	int step;
	bool friend operator<(node a,node b)
	{
		return a.step>b.step;//让队列以step从小到大排序,咱也不知道为啥这样写
	}
};
int check(node o)//看是否越界或上墙 
{
	if(o.x>=0&&o.y>=0&&o.x<n&&o.y<m&&map[o.x][o.y]!='#')
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
int bfs()
{
	struct node now;
	struct node next;
	now.x=sx;
	now.y=sy;
	now.step=0;
	priority_queue<node>q;
	q.push(now);
	while(!q.empty())
	{
		now=q.top();
		q.pop();
		if(now.x==ex&&now.y==ey)
		{
			return now.step;
		}
		for(int i=0;i<4;i++)
		{
			next.x=now.x+dx[i];
			next.y=now.y+dy[i];
			if(check(next))
			{
				if(map[next.x][next.y]=='.'||map[next.x][next.y]=='r')
				{
					next.step=now.step+1;
				}
				if(map[next.x][next.y]=='x')
				{
					next.step=now.step+2;
				}
				map[next.x][next.y]='#';
				q.push(next);
			}
		}
	}
	return -1;
}
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		for(int i=0;i<n;i++)
		{
			scanf("%s",map[i]);
			for(int j=0;j<m;j++)
			{
				if(map[i][j]=='a')
				{
					sx=i;
					sy=j;
				}
				if(map[i][j]=='r')
				{
					ex=i;
					ey=j;
				}
			}
		}
		int sum=bfs();
		if(sum==-1)
		{
			printf("Poor ANGEL has to stay in the prison all his life.\n");
		}
		else
		{
			printf("%d\n",sum);
		}
	}
	return 0;
}

更新下最近集训又写了一遍:

#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
int n,m;
char map[210][210];
int visit[210][210];
int sn,sm,en,em;
struct node
{
	int n,m,step;
	friend bool operator<(node x,node y)
	{
		return x.step>y.step;
	}
};
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		memset(visit,0,sizeof(visit));
		for(int i=0;i<n;i++)
		{
			scanf("%s",&map[i]);
			for(int j=0;j<m;j++)
			{
				if(map[i][j]=='a')
				{
					sn=i;
					sm=j;
					visit[i][j]=1;
				}
				if(map[i][j]=='r')
				{
					en=i;
					em=j;
				}
			}
		}
		priority_queue<node>q;
		struct node start;
		start.n=sn;
		start.m=sm;
		start.step=0;
		q.push(start);
		int flag=0;
		while(!q.empty())
		{
			struct node tmp;
			tmp=q.top();
			q.pop();
			if(tmp.n==en&&tmp.m==em)
			{
				printf("%d\n",tmp.step);
				flag=1;
				break;
			}
			tmp.step++;
			if(tmp.n-1>=0&&map[tmp.n-1][tmp.m]!='#'&&visit[tmp.n-1][tmp.m]==0)
			{
				visit[tmp.n-1][tmp.m]=1;
				struct node tmp2=tmp;
				tmp2.n--;
				if(map[tmp2.n][tmp2.m]=='x')
				{
					tmp2.step++;
				}
				q.push(tmp2);
			}
			if(tmp.n+1<n&&map[tmp.n+1][tmp.m]!='#'&&visit[tmp.n+1][tmp.m]==0)
			{
				visit[tmp.n+1][tmp.m]=1;
				struct node tmp2=tmp;
				tmp2.n++;
				if(map[tmp2.n][tmp2.m]=='x')
				{
					tmp2.step++;
				}
				q.push(tmp2);
			}
			if(tmp.m-1>=0&&map[tmp.n][tmp.m-1]!='#'&&visit[tmp.n][tmp.m-1]==0)
			{
				visit[tmp.n][tmp.m-1]=1;
				struct node tmp2=tmp;
				tmp2.m--;
				if(map[tmp2.n][tmp2.m]=='x')
				{
					tmp2.step++;
				}
				q.push(tmp2);
			}
			if(tmp.m+1<m&&map[tmp.n][tmp.m+1]!='#'&&visit[tmp.n][tmp.m+1]==0)
			{
				visit[tmp.n][tmp.m+1]=1;
				struct node tmp2=tmp;
				tmp2.m++;
				if(map[tmp2.n][tmp2.m]=='x')
				{
					tmp2.step++;
				}
				q.push(tmp2);
			}
		}
		if(flag==0)
		{
			printf("Poor ANGEL has to stay in the prison all his life.\n");
		}
	}
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值