hdu 1242 Rescue

Rescue

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


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
题解:天使被抓进监狱,ta的朋友们(r)要去解救ta,(注意:是朋友们~~~)每个人可以走的方向有上、下、左、右四种,有些地方可能还会有守卫(x),杀死一个守卫需要一单位的时间,每到达一个新地点也需要一单位的时间,求要救出天使需要的最少时间。 (由于朋友可能不只一个,所以我们可以假设让天使去找ta的朋友们,只要找到一个朋友,即被解救!) ---BFS---
<pre name="code" class="cpp"><span style="font-family:SimSun;font-size:18px;"><span style="font-size:18px;"></span>
<span style="font-family:SimSun;font-size:18px;">#include<stdio.h>
#include<queue>
#include<functional>
#include<string.h>
using namespace std;
int n,m,sx,sy,ex,ey;
int vis[210][210];
char s[210][210];
int dx[4]={1,-1,0,0}; //表示四个方向 
int dy[4]={0,0,1,-1};
int go(int x,int y)  
{  
    if(0<=x&&x<n&&0<=y&&y<m&&s[x][y]!='#')  
    	return 1;  
    return 0;  
}  
struct node
{
	int x,y,step;
	friend bool operator < (node a,node b)
	{
		return a.step>b.step;
	}
};
int bfs(int x,int y)
{
	priority_queue<node>que;  //对时间进行排序,时间短的优先级高 
	node p;
	p.x=x;
	p.y=y;
	p.step=0;
	que.push(p);
	while(!que.empty())
	{
		node q1;
		p=que.top();
		que.pop();
		if(s[p.x][p.y]=='r')  //找到朋友 
			return p.step;
		for(int i=0;i<4;i++)
		{
			q1.x=p.x+dx[i];
			q1.y=p.y+dy[i];
			if(go(q1.x,q1.y)&&!vis[q1.x][q1.y])
			{
				vis[q1.x][q1.y]=1;   //对走过的路进行标记,标记后则不再记录 
				if(s[q1.x][q1.y]=='x')
					q1.step=p.step+2;  //到达此地但有守卫,花费两单位的时间 
				else 
					q1.step=p.step+1;  //无守卫 
				que.push(q1);
			}
		}
	}
	return 0;
}
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		memset(s,0,sizeof(s));
		memset(vis,0,sizeof(vis));
		getchar();
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				scanf("%c",&s[i][j]);
				if(s[i][j]=='a')
				{
					sx=i;sy=j;
				}
			}
			getchar();
		}
		vis[sx][sy]=1;
		int ans=bfs(sx,sy);
		if(!ans)
			printf("Poor ANGEL has to stay in the prison all his life.\n");
		else
			printf("%d\n",ans);
	}
	return 0;
}</span>
</span>


 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值