杭电1242--Rescue(BFS+优先队列)

Rescue

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

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

//第一道这样的题,思路大致了解 。

 1 #include <queue>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 using namespace std;
 6
 7
 8 int dir[4][2]={-1, 0, 1, 0, 0, -1, 0, 1};
 9 char map[220][220];  int vis[220][220];
10 int i, j, m, n;
11
12 struct prision
13 {
14     int x, y, time;
15     friend bool operator < (prision x, prision y)
16     {
17         return x.time > y.time;      //从小  →  大;
18     }
19 } current, next;
20
21
22 int BFS(int x,int y)
23 {
24
25
26     priority_queue <prision>q;
27     prision current,next;
28     memset(vis,0,sizeof(vis));
29
30     current.x=x;
31     current.y=y;
32     current.time=0;
33     vis[current.x][current.y]=1;
34     q.push(current);
35
36
37     while(!q.empty())
38     {
39
40         current=q.top();
41         q.pop();
42         for(int i=0;i<4;i++)
43         {
44             next.x=current.x+dir[i][0];
45             next.y=current.y+dir[i][1];
46
47             if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&map[next.x][next.y]!=‘#‘&&!vis[next.x][next.y])
48             {
49
50                 if(map[next.x][next.y]==‘r‘)
51                     return current.time+1;
52
53                 if(map[next.x][next.y]==‘x‘)
54                     next.time=current.time+2;
55                 else
56                     next.time=current.time+1;
57                 vis[next.x][next.y]=1;
58                 q.push(next);
59             }
60         }
61     }
62     return -1;
63 }
64
65
66 int main()
67 {
68     prision angle;
69     while(~scanf("%d %d", &n, &m))
70     {
71         for(i=0; i<n; i++)
72         {
73             for(j=0; j<m; j++)
74             {
75                 cin >> map[i][j];
76                 if(map[i][j] == ‘a‘)
77                 {
78                     angle.x = i;
79                     angle.y = j;
80                 }
81             }
82         }
83         int time = BFS(angle.x, angle.y);
84
85         if(time == -1)
86             cout <<"Poor ANGEL has to stay in the prison all his life."<<endl;
87         else
88             cout <<time<< endl;
89     }
90     return 0;
91 }  
时间: 2024-08-28 11:04:40

杭电1242--Rescue(BFS+优先队列)的相关文章

杭电ACM1242——Rescue~~BFS+优先队列

这题,简单的BFS就可以搞定.题目的大概意思是最短时间从地图的r到达a. 一开始,用普通的队列来做,结果内存超了,原因是N和M最大200:普通的队列会浪费一大堆内存,所以应该改用优先队列来做. 下面是AC的代码: #include <iostream> #include <queue> #include <cstdio> using namespace std; class data { public: int x, y, cost; friend bool opera

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

[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(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(优先队列)

题意: 一个天使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;

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!

poj1649 Rescue(BFS+优先队列)

Rescue Time Limit: 2 Seconds      Memory Limit: 65536 KB 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

HDU 1242 (BFS+优先队列)

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