hdoj 1242 Rescue (BFS)

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

ZOJ Monthly, October 2003

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<cstring>

#include<cstdlib>

#include<queue>

#include<algorithm>

using namespace std;

const int M=205;

int n,m;

int start_x,end_x;

int start_y,end_y;

char map[M][M];

int cost[M][M];

int fx[4]={1,0,-1,0};

int fy[4]={0,1,0,-1};

typedef struct

{

int x,y;

int time;

}point;

void init()//

{

int i,j;

for(i=0;i<n;i++)

for(j=0;j<m;j++)

{

if(map[i][j]==‘r‘)// 接入端

{

start_x=i;

start_y=j;

}

if(map[i][j]==‘a‘)//输出端

{

end_x=i;

end_y=j;

}

cost[i][j]=-1;

}

}

int isvalid(int x,int y)

{

if(x>=0&&x<n&&y>=0&&y<m)

return 1;

else

return 0;

}

void BFS()

{

point p1,p2;

queue<point> Q;//定义队列

int i;

p1.x=start_x;//初始化

p1.y=start_y;

p1.time=0;

cost[p1.x][p1.y]=0;

while(!Q.empty())//初始化

Q.pop();

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

while(!Q.empty())//当队列不为空

{

p1=Q.front();//取出队头结点信息

Q.pop();

for(i=0;i<4;i++)//满足条件

{

p2.x=p1.x+fx[i];

p2.y=p1.y+fy[i];

p2.time=p1.time+1;

if(map[p2.x][p2.y]==‘#‘||!isvalid(p2.x,p2.y))//满足条件

continue;

if(map[p2.x][p2.y]==‘x‘)

p2.time++;

if(cost[p2.x][p2.y]==-1||p2.time<cost[p2.x][p2.y])

{

Q.push(p2);//入队

cost[p2.x][p2.y]=p2.time;

}

}

}

}

int main()

{

while(~scanf("%d%d",&n,&m))

{

int i,j;

for(i=0;i<n;i++)

scanf("%s",map[i]);

init();

BFS();

if(cost[end_x][end_y]==-1)

printf("Poor ANGEL has to stay in the prison all his life.\n");

else

printf("%d\n",cost[end_x][end_y]);

}

return 0;

}

时间: 2024-08-28 10:02:26

hdoj 1242 Rescue (BFS)的相关文章

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue 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:

BFS HDOJ 1242 Rescue

题目传送门 题意:从r走到a,遇到x多走一步,问最小走到a的步数 分析:因为r有多个,反过来想从a走到某个r的最小步数,简单的BFS.我对这题有特殊的感情,去年刚来集训队时肉鸽推荐了这题,当时什么都不会,看个数组模拟队列的BFS看的头晕,现在看起来也不过如此,额,当年开始是从r走到a的,因为数据巨弱才过的,应该要用到优先队列. /************************************************ * Author :Running_Time * Created Ti

hdu 1242 Rescue(bfs+优先队列)

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

HDU 1242 Rescue(BFS)

题目链接 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: a

1242 Rescue BFS

#include<iostream> #include<string> #include<string.h> #include<stdio.h> #include<math.h> #include<queue> #include<algorithm> using namespace std; int n,m,di,dj,ok,ss; char mapp[210][210]; int vis[210][210]; //应该不

HDU 1242 Rescue(优先队列+bfs)

题目地址:HDU 1242 这个题相比于普通的bfs有个特殊的地方,经过士兵时会额外消耗时间,也就是说此时最先搜到的时候不一定是用时最短的了.需要全部搜一遍才可以.这时候优先队列的好处就显现出来了.利用优先队列,可以让队列中的元素按时间排序,让先出来的总是时间短的,这样的话,最先搜到的一定是时间短的,就不用全部搜一遍了.PS:我是为了学优先队列做的这题..不是为了这题而现学的优先队列.. 代码如下: #include <iostream> #include <stdio.h> #i

hdu 1242:Rescue(BFS广搜 + 优先队列)

Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 14   Accepted Submission(s) : 7 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Angel was caught by the MOLIGPY

HDU 1242 Rescue营救 BFS算法

题目链接:HDU 1242 Rescue营救 Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16524    Accepted Submission(s): 5997 Problem Description Angel was caught by the MOLIGPY! He was put in prison by

HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 开始以为是水题,想敲一下练手的,后来发现并不是一个简单的搜索题,BFS做肯定出事...后来发现题目里面也有坑 题意是从r到a的最短距离,"."相当时间单位1,"x"相当时间单位2,求最短时间 HDU 搜索课件上说,这题和HDU1010相似,刚开始并没有觉得像剪枝,就改用  双向BFS   0ms  一Y,爽! 网上查了一下,神牛们竟然用BFS+