hdoj 1242 Rescue (BFS)

110 篇文章 0 订阅
8 篇文章 0 订阅

Rescue

http://acm.hdu.edu.cn/showproblem.php?pid=1242
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18962    Accepted Submission(s): 6771


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   |   We have carefully selected several similar problems for you:  1240 1016 1010 1072 1253 
 
/**/第一次写 BFS 

题目大意:

 1.在含有障碍的迷宫中求从一点到一点的最短时间或步数,

 2.这个题目中,天使的朋友不止一个,所以搜索的起点应该是从天使开始搜到朋友为止,

 3.题目求的是最短时间,所以应该才用优先队列,小步数优先。

----------------------------------------------------------------------------------------------------------------------------------------

/**/BFS:

1.从图中的某一项V0开始,先访问V0;

2.访问所有与v0相邻接的顶点 v1、v2、、、、、、Vt;

3.依次访问与 v1、v2、、、、、、Vt 相邻接的所有未曾访问过的顶点;

4.循此以往,直至所有的顶点都被访问过为止;


BFS具体操作:

      定义一个队列;          queue<node> Q;

     起始点加入队列;       Q.push(p);

    while(队列不空)            while(!Q.empty())

   {                                        {

      取出队头结点;             p=Q.front; Q.pop;

     若是所求的目标状态      if(满足条件)

          跳出循环;                        return ; 

    否则                                   else

        从它扩展出子结点,      

        全部添加到队尾中               Q.push();
   }                                         }

若循环中找到目标,输出结果;否则,输出无解;

----------------------------------------------------------------------------------------------------------------------------------------

#include<cstdio>
#include<cmath>
#include<queue>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
#define MAX 205
char map[MAX][MAX];
int step[MAX][MAX];

int f[2][4]={1,0,-1,0,0,1,0,-1};
int n,m; 	
int sx,sy,ex,ey;
struct node
{
	int x;
	int y;
	int time;
};

queue<node>q;
void input()
{
	for(int i=0;i<n;i++)
	 for(int j=0;j<m;j++)
	 {
	 	if(map[i][j]=='r')//朋友
	 	{
	 	 sx=i;
		 sy=j;	
		}
		if(map[i][j]=='a')//天使
		{
		  ex=i;
		  ey=j;
		}
		step[i][j]=-1;
	 }
}

void BFS()
{
	node p1,p2;
	p1.x=sx;
	p1.y=sy;
	p1.time=0;
	step[p1.x][p1.y]=0;
	
	while(!q.empty())
	q.pop();
	
	q.push(p1);
	while(!q.empty())
    {
      p1=q.front();
      q.pop();
      
      for(int i=0;i<4;i++)
      {
      	p2.x=p1.x+f[0][i];
      	p2.y=p1.y+f[1][i];
      	
      	if(map[p2.x][p2.y]!='#'&&(p2.x>=0&&p2.x<n&&p2.y>=0&&p2.y<m))//边界
      	{
      	          p2.time=p1.time+1;//行走一步需要1s
		  if(map[p2.x][p2.y]=='x')//杀一个守卫需要1s
		  p2.time++;
		  if(step[p2.x][p2.y]==-1||p2.time<step[p2.x][p2.y])
		  {
			q.push(p2);
			step[p2.x][p2.y]=p2.time; 
		  }	
		}
	  }
	}
}
int main()
{
   while(~scanf("%d%d",&n,&m))
   {
   	for(int i=0;i<n;i++)
   	scanf("%s",map[i]);
   	input();
   	BFS();
   	if(step[ex][ey]==-1)
   	printf("Poor ANGEL has to stay in the prison all his life.\n");
   	else
   	printf("%d\n",step[ex][ey]);
   }
   return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值