hdu1242 优先队列+bfs

Rescue

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

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 1241

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <queue>
 4 #include <algorithm>
 5 using namespace std;
 6
 7 struct Point
 8 {
 9     int x,y,t;
10     bool operator < (const Point &a)const
11     {
12         return t>a.t;
13     }
14 };
15
16 char map[205][205];
17 Point start;
18 int n,m;
19 int diect[][2]={{1,0},{-1,0},{0,1},{0,-1}};
20
21 int bfs()
22 {
23     priority_queue<Point> que;
24     Point cur,next;
25     int i,j;
26
27     map[start.x][start.y]=‘#‘;
28     que.push(start);
29     while(!que.empty())
30     {
31         cur=que.top();
32         que.pop();
33         for(i=0;i<4;i++)
34         {
35             next.x=cur.x+diect[i][0];
36             next.y=cur.y+diect[i][1];
37             next.t=cur.t+1;
38             if(next.x<0 || next.x>=n || next.y<0 || next.y>=m)
39                 continue;
40             if(map[next.x][next.y]==‘#‘)
41                 continue;
42             if(map[next.x][next.y]==‘r‘)
43                 return next.t;
44             if(map[next.x][next.y]==‘.‘)
45             {
46                 map[next.x][next.y]=‘#‘;
47                 que.push(next);
48             }
49             else if(map[next.x][next.y]==‘x‘)
50             {
51                 map[next.x][next.y]=‘#‘;
52                 next.t++;
53                 que.push(next);
54             }
55         }
56     }
57     return -1;
58 }
59
60 int main()
61 {
62     int i,j,ans;
63     char *p;
64     while(scanf("%d %d",&n,&m)!=EOF)
65     {
66         for(i=0;i<n;i++)
67         {
68             scanf("%s",map[i]);
69             if(p=strchr(map[i],‘a‘))
70             {
71                 start.x=i;
72                 start.y=p-map[i];
73                 start.t=0;
74             }
75         }
76         ans=bfs();
77         if(ans==-1)
78             printf("Poor ANGEL has to stay in the prison all his life.\n");
79         else
80             printf("%d\n",ans);
81     }
82     return 0;
83 }

时间: 2024-12-22 13:49:20

hdu1242 优先队列+bfs的相关文章

HDU1242 (BFS搜索中使用优先队列)

一道用到优先队列的BFS题目 #include <iostream> #include <string> #include <cstdio> #include <cstring> #include <queue> #define N 201 using namespace std; char maze[N][N]; int a,b,anw; bool visit[N][N]; int dir[4][2]={{0,1},{1,0},{-1,0},{

HDU 1242 Rescue(优先队列+bfs)

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

Ignatius and the Princess I (hdu 1026 优先队列+bfs+输出路径)

Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14624    Accepted Submission(s): 4634 Special Judge Problem Description The Princess has been abducted by the BEelzeb

POJ 2312 Battle City(优先队列+BFS)

题目链接:http://poj.org/problem?id=2312 Battle City Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7085   Accepted: 2390 Description Many of us had played the game "Battle city" in our childhood, and some people (like me) even often pl

poj 2251 Dungeon Master(优先队列bfs)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20867   Accepted: 8090 Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled

hdu 5040 优先队列BFS+剪枝

(北京网络赛09题)题意:给一矩阵(图),里面有起点,终点,还有探照灯(每个有初始朝向,每秒顺时针转90度),前面有灯或者自己被灯照着,移动就要花3秒,求起点到终点最短时间. 用一个数组三维数组记录一下,用来当前位置当前时间%4有没有灯,然后优先队列(时间短的在前面),搜索即可.考虑到可以来回走或者原地等,不能简单判重剪枝:每个地方最多是4种状态!就是4秒之后就全图状态回到一样!所以若当前状态(时间%4)下来过就不用来了. #include<iostream> #include<vect

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;

[hdu1242]优先队列

题意:给一个地图,'x'走一步代价为2,'.'走一步代价为1,求从s到t的最小代价.裸优先队列. 1 #pragma comment(linker, "/STACK:10240000,10240000") 2 3 #include <iostream> 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstdlib> 7 #include <cstring> 8 #

hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)

以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要花费1s+数字大小的时间. 比较麻烦的是需要记录路径.还要记录是在走路还是在打怪. 因为求最短路,所以可以使用bfs. 因为进过每一个点花费时间不同,所以可以使用优先队列. 因为需要记录路径,所以需要开一个数组,来记录经过节点的父节点.当然,记录方法不止一种. 上代码—— 1 #include <c