Rescue HDU - 1242

题目

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

1
2
3
4
5
6
7
8
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........

Sample Output

1
13

思路

这道题有几个需要注意的地方。
1、有多个朋友,也就是说有多个起点,需要进行多次bfs,最后求最短路径。
2、在路上,有守卫的地方,所花费的时间为2(击杀守卫通过路径),需要利用一个优先队列,先遍历花费时间为1的再遍历花费时间为2的。
优先队列比较算子定义方法,tql。

1
2
3
4
5
6
7
#include<queue>
struct node{
int x,y,dep;
node(int x,int y,int dep):x(x),y(y),dep(dep){}
bool operator < (const node &a) const {return dep>a.dep;}
};
priority_queue<node> qu;

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int MAXSIZE=210;
int visit[MAXSIZE][MAXSIZE];
int sx[MAXSIZE]={0},sy[MAXSIZE]={0},ex=0,ey=0;
string str[MAXSIZE];
int dx[]={1,0,0,-1};
int dy[]={0,1,-1,0};
int N,M;
int ans=0x3f3f3f3f;

struct node{
int x,y,dep;
node(int x,int y,int dep):x(x),y(y),dep(dep){}
bool operator < (const node &a) const {return dep>a.dep;}
};
void BFS(int x,int y){
memset(visit,0,sizeof(visit));
priority_queue<node> qu;
qu.push(node(x,y,0));
visit[x][y]=1;
while(!qu.empty()){
node t=qu.top();qu.pop();
for(int i=0;i<4;i++){
int tx=t.x+dx[i];
int ty=t.y+dy[i];
if(tx<0||tx>=N||ty<0||ty>=M||visit[tx][ty]==1||str[tx][ty]=='#')continue;
visit[tx][ty]=1;
if(tx==ex&&ty==ey){ans=min(t.dep+1,ans);return;}
if(str[tx][ty]=='x')
qu.push(node(tx,ty,t.dep+2));
else qu.push(node(tx,ty,t.dep+1));

}
}
}
int main(){
while(cin>>N>>M){
ans=0x3f3f3f3f;
vector<node> vc;
for(int i=0;i<N;i++) cin>>str[i];
for(int i=0;i<N;i++){
for(int j=0;j<M;j++){
if(str[i][j]=='r'){vc.push_back(node(i,j,0));}
if(str[i][j]=='a'){ex=i;ey=j;}
}
}
for(int i=0;i<vc.size();i++)
BFS(vc[i].x,vc[i].y);
//cout<<sx<<' '<<sy<<' '<<ex<<' '<<ey<<endl;
if(ans==0x3f3f3f3f) cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
else cout<<ans<<endl;
}
return 0;
}
-------------本文结束感谢您的阅读-------------
0%