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

bfs+优先队列...

bfs能找到最优解的原因是...时间更短的点一定是先访问到先入队...

但是由于守卫的存在...先访问到的节点如果是守卫的话就不是时间更短的节点...

所以这个时候我们要用优先队列...

第一次写.

然后学习了重载<的写法...写在结构体里面和外面的..

感觉q老师...窝只是请教他这个重载在写结构体里面要怎样...他就把kuangbin的模板发了我一份

学到了不少.

还有就是,因为朋友有多个,而小天使只有一个...

不如让小天使去找朋友...逆向思维...

一遍ac,开心.

  1 /*************************************************************************
  2     > File Name: code/hdu/1242.cpp
  3     > Author: 111qqz
  4     > Email: [email protected]
  5     > Created Time: 2015年10月02日 星期五 14时38分17秒
  6  ************************************************************************/
  7
  8 #include<iostream>
  9 #include<iomanip>
 10 #include<cstdio>
 11 #include<algorithm>
 12 #include<cmath>
 13 #include<cstring>
 14 #include<string>
 15 #include<map>
 16 #include<set>
 17 #include<queue>
 18 #include<vector>
 19 #include<stack>
 20 #include<cctype>
 21 using namespace std;
 22 #define yn hez111qqz
 23 #define j1 cute111qqz
 24 #define ms(a,x) memset(a,x,sizeof(a))
 25 #define lr dying111qqz
 26 const int dx4[4]={1,0,0,-1};
 27 const int dy4[4]={0,-1,1,0};
 28 typedef long long LL;
 29 typedef double DB;
 30 const int inf = 0x3f3f3f3f;
 31 const int N=2E2+5;
 32 char maze[N][N];
 33 int m,n;
 34 int ans;
 35 bool vis[N][N];
 36 struct node
 37 {
 38     int x,y;
 39     int d;
 40
 41     bool ok ()
 42     {
 43     if (x>=0&&x<n&&y>=0&&y<m&&!vis[x][y]&&maze[x][y]!=‘#‘) return true;
 44     return false;
 45     }
 46     bool hasguard()
 47     {
 48     if (maze[x][y]==‘x‘) return true;
 49     return false;
 50     }
 51     bool goal()
 52     {
 53     if (maze[x][y]==‘r‘) return true;
 54     return false;
 55     }
 56 }s;
 57
 58 bool operator<(const node &a,const node &b)    //重载优先级的<关系...返回true时优先级更小,在队列的更后面.
 59 {
 60     return a.d>b.d;
 61 }
 62
 63 bool bfs()
 64 {
 65     priority_queue<node>q;
 66     q.push(s);
 67
 68     while (!q.empty())
 69     {
 70     node pre = q.top();
 71     q.pop();
 72 //    cout<<pre.x<<" "<<pre.y<<" "<<pre.d<<endl;
 73     if (pre.goal())
 74     {
 75         ans = pre.d;
 76         return true;
 77     }
 78
 79     for ( int i = 0 ; i < 4 ; i++)
 80     {
 81         node next;
 82         next.x = pre.x + dx4[i];
 83         next.y = pre.y + dy4[i];
 84         next.d = pre.d + 1;
 85         if (!next.ok()) continue;
 86         vis[next.x][next.y] = true;
 87         if (next.hasguard()) next.d++;
 88         q.push(next);
 89
 90
 91     }
 92     }
 93     return false;
 94 }
 95 int main()
 96 {
 97   #ifndef  ONLINE_JUDGE
 98    freopen("in.txt","r",stdin);
 99   #endif
100     while(scanf("%d %d",&n,&m)!=EOF){
101     ms(vis,false);
102     for ( int i = 0 ; i < n ; i++) scanf("%s",maze[i]);
103     for ( int i = 0 ; i < n ; i++)
104     {
105     for ( int j = 0 ; j < m ; j++)
106     {
107         if (maze[i][j]==‘a‘)
108         {
109         s.x = i ;
110         s.y = j ;
111         s.d = 0 ;
112         vis[i][j] = true;
113         break;
114         }
115     }
116     }
117
118     if (bfs())
119     {
120     printf("%d\n",ans);
121     }
122     else
123     {
124     puts("Poor ANGEL has to stay in the prison all his life.");
125     }
126 }
127  #ifndef ONLINE_JUDGE
128   fclose(stdin);
129   #endif
130     return 0;
131 }

时间: 2024-10-23 07:54:05

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:

HDU 1242——Rescue(优先队列)

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

hdu 1242 rescue (优先队列 bfs)

题意: 公主被关在 a位置 她的朋友在r位置 路上x位置有恶魔 遇上恶魔花费2 时间 否在时间花费 1 时间 问 最短多少时间 找到公主 思路: bfs+ 优先队列(时间短的先出列) #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<iostream> #include<algorithm> using namespace std;

HDU 1242 (BFS+优先队列)

题意:X代表卫兵,a代表终点,r代表起始点,.代表路,#代表墙,走过.要花费一秒,走过x要花费2秒,求从起点到终点的最少时间. 析:一看到样例就知道是BFS了吧,很明显是最短路径问题,不过又加了一个条件——时间,所以我们用优先队列去优先获取时间短的路径,总体实现起来没有太大难度. 代码如下: #include <iostream> #include <cstdio> #include <vector> #include <set> #include <

HDU 1242 rescue and 优先队列入门

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

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广搜 + 优先队列)

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)&amp;&amp;( BFS+优先队列)

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