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



  BFS广搜 + 优先队列

  题意:从前有一名天使被囚禁了,天使的朋友要去救他,你的任务是输出最短的救援时间。天使的朋友每秒可以走一步,地牢中有守卫,当你遇到守卫的时候需要停留一秒杀死守卫。给你地牢的地图,上面有几种元素,‘.‘表示路,可以通行。‘#‘代表墙,无法通行。‘r‘表示天使的朋友,代表起点。‘a‘表示天使的位置,代表终点。‘x‘表示守卫的位置。

  思路:因为是求“最短路径”的问题,正好用广搜可以解决。但是与普通广搜不同的是,遇到守卫的时候会多停留一秒。这就会导致队列中优先级的不稳定,所以需要用优先队列,让优先级最高的节点始终在队列最前面。

  代码


 1 #include <iostream>
2 #include <string.h>
3 #include <queue>
4 using namespace std;
5 char a[201][201];
6 bool isv[201][201]; //记录访问过没有
7 int dx[4] = {0,1,0,-1};
8 int dy[4] = {1,0,-1,0};
9 int N,M;
10 int sx,sy,ex,ey;
11 struct NODE{
12 int x;
13 int y;
14 int step;
15 friend bool operator < (NODE n1,NODE n2) //自定义优先级。在优先队列中,优先级高的元素先出队列。
16 {
17 return n1.step > n2.step; //通过题意可知 step 小的优先级高,需要先出队。
18 }
19 };
20 bool judge(int x,int y)
21 {
22 if( x<1 || y<1 || x>N || y>M )
23 return 1;
24 if( isv[x][y] )
25 return 1;
26 if( a[x][y]==‘#‘ )
27 return 1;
28 return 0;
29 }
30 int bfs() //返回从(x,y)开始广搜,到右下角的最短步数,如果无法到达右下角,返回0
31 {
32 memset(isv,0,sizeof(isv));
33 priority_queue <NODE> q; //定义一个优先队列
34 NODE cur,next;
35 cur.x = sx;
36 cur.y = sy;
37 cur.step = 0;
38 isv[sx][sy] = true;
39 q.push(cur); //第一个元素入队
40 while(!q.empty()){
41 cur = q.top(); //队首出队,注意不是front()
42 q.pop();
43 if(cur.x==ex && cur.y==ey) //到终点
44 return cur.step;
45 for(int i=0;i<4;i++){
46 int nx = cur.x + dx[i];
47 int ny = cur.y + dy[i];
48 if( judge(nx,ny) ) //判定
49 continue;
50 //可以走
51 next.x = nx;
52 next.y = ny;
53 if(a[nx][ny]==‘x‘)
54 next.step = cur.step + 2;
55 else
56 next.step = cur.step + 1;
57 isv[nx][ny] = true;
58 q.push(next);
59 }
60 }
61 return -1;
62 }
63 int main()
64 {
65 while(cin>>N>>M){
66 for(int i=1;i<=N;i++) //输入
67 for(int j=1;j<=M;j++){
68 cin>>a[i][j];
69 if(a[i][j]==‘a‘)
70 ex=i,ey=j;
71 else if(a[i][j]==‘r‘)
72 sx=i,sy=j;
73 }
74 int step = bfs();
75 if(step==-1) //不能到达
76 cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
77 else
78 cout<<step<<endl;
79 }
80 return 0;
81 }

Freecode : www.cnblogs.com/yym2013

hdu 1242:Rescue(BFS广搜 + 优先队列),码迷,mamicode.com

时间: 2024-12-04 15:41:46

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

杭电 1242 Rescue(广搜)

http://acm.hdu.edu.cn/showproblem.php?pid=1242 Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15597    Accepted Submission(s): 5663 Problem Description Angel was caught by the MOLIGPY!

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

HDU 1242 Rescue(优先队列+bfs)

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

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

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

HDU 1242——Rescue(优先队列)

题意: 一个天使a被关在迷宫里,她的许多小伙伴r打算去救她,求小伙伴就到她需要的最小时间.在迷宫里有守卫,打败守卫需要一个单位时间,如果碰到守卫必须要杀死他 思路: 天使只有一个,她的小伙伴有很多,所以可以让天使找她的小伙伴,一旦找到小伙伴就renturn.时间小的优先级高.优先队列搞定 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<

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 1195:Open the Lock(暴力BFS广搜)

mediaxyz是一位研究ffmpeg有三年的高人了,这几天一直在折腾ffmpeg中的x264,就是不知道该如何控制码率,主要是参数太多,也不知道该如何设置,在 google上search了一下,这方面的介绍为0,那就找mediaxyz请教请教吧,这些可都是经验,非常宝贵! 以下是与mediaxyz在QQ上聊天的记录,只有一部分,因为QQ把之前的谈话删除了,但基本上精髓都可这里了. mediaxyz 23:40:26你说的qsable是c->global_quality吧 Leon 23:40: