HDU 1242 Rescue(BFS+优先队列)

Posted wenzhixin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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: 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模板,后来发现拓展每一层后如果有士兵的话,需要花费两秒,造成该路径与同层拓展的路径的时间优先级不同了,自然就想到了使用优先队列进行搜索。
代码:
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<queue>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 struct node{
 8     int x,y,s;
 9     bool operator < (const node &a) const {
10     return a.s<s;};
11 };
12 bool bk[210][210]; 
13 char map[210][210];
14 void bfs(int sx,int sy);
15 int n,m,ex,ey;
16 
17 int main()
18 {
19     //freopen("E:\testin.txt","r",stdin);
20     int sx,sy;
21     while(scanf("%d%d",&n,&m) != EOF){
22         memset(map,0,sizeof(map));
23         
24         for(int i=1;i<=n;i++){
25             for(int j=1;j<=m;j++){
26                 scanf(" %c",&map[i][j]);
27                 if(map[i][j] == r)
28                 {
29                     sx=i;sy=j;
30                 }
31             }
32         }
33         
34         bfs(sx,sy);
35     }
36     return 0;
37 }
38 
39 void bfs(int sx,int sy){
40     priority_queue<struct node> q;
41     struct node head,temp,temp1;
42     memset(bk,0,sizeof(bk));
43     head.x=sx;
44     head.y=sy;
45     head.s=0;
46     int next[4][2]={-1,0,0,1,1,0,0,-1};
47     q.push(head);
48     bk[sx][sy]=1;
49     int flag=0,tx,ty;
50     
51     while(!q.empty()){
52         temp=q.top();q.pop();
53         
54         for(int i=0;i<4;i++){
55             tx=temp.x+next[i][0];
56             ty=temp.y+next[i][1];
57             
58             if(tx < 1 || tx > n || ty < 1 || ty > m || map[tx][ty] == # || map[tx][ty] == r)
59                 continue;
60             if(bk[tx][ty] == 0){
61                 bk[tx][ty] = 1;
62                 
63                 if(map[tx][ty] == .){
64                     temp1.x=tx;
65                     temp1.y=ty;
66                     temp1.s=temp.s+1;
67                     q.push(temp1);
68                 }
69                 if(map[tx][ty] == x){
70                     temp1.x=tx;
71                     temp1.y=ty;
72                     temp1.s=temp.s+2;
73                     q.push(temp1);
74                 }
75                 if(map[tx][ty] == a){
76                     flag=1;
77                     printf("%d
",temp.s+1);
78                 }
79             }
80         }
81     }
82     if(flag == 0)
83         printf("Poor ANGEL has to stay in the prison all his life.
");
84 }

 

 








以上是关于HDU 1242 Rescue(BFS+优先队列)的主要内容,如果未能解决你的问题,请参考以下文章

hdu 1242 Rescue (BFS+优先队列)

HDU 1242 Rescue BFS+优先队列

hdu Rescue (bfs)

[ACM] hdu 1242 BFS

HDU 1242 Rescue(BFS)

HDU 1242 (BFS+优先队列)