zoj1649-Rescue (迷宫最短路径)【bfs 优先队列】

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=649

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

题意:从'r'起步,目的地为'a','#'不可过,'.'为一步,特殊的是‘x'算两步。求最少步数。

思路:bfs。这里需要注意的是x,因为算两步,在普通的bfs队列里不一定先出列的是最短步,所以得用优先队列。

代码:

 1 #include <fstream>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cstdio>
 5 #include <cstring>
 6 #include <cmath>
 7 #include <cstdlib>
 8 #include <queue>
 9 
10 using namespace std;
11 
12 #define PI acos(-1.0)
13 #define EPS 1e-10
14 #define lll __int64
15 #define ll long long
16 #define INF 0x7fffffff
17 
18 struct node{
19     int x,y,step;
20     bool operator < (const node &r) const{
21         return step>r.step;
22     }
23 }tmp;
24 
25 priority_queue<node> qu;
26 int n,m,x1,y1_;
27 char matrix[205][205];
28 bool b[205][205];
29 char xy[2][4]={{0,0,1,-1},{1,-1,0,0}};
30 
31 inline bool Check(int x,int y);
32 int Bfs();
33 
34 int main(){
35     //freopen("D:\input.in","r",stdin);
36     //freopen("D:\output.out","w",stdout);
37     while(~scanf("%d %d",&n,&m)){
38         for(int i=0;i<=m+1;i++)    matrix[0][i]='#',matrix[n+1][i]='#';
39         for(int i=0;i<=n+1;i++)    matrix[i][0]='#',matrix[i][m+1]='#';
40         for(int i=1;i<=n;i++){
41             getchar();
42             for(int j=1;j<=m;j++){
43                 matrix[i][j]=getchar();
44                 if(matrix[i][j]=='r')  x1=i,y1_=j;
45             }
46         }
47         int ans=Bfs();
48         if(ans==-1) puts("Poor ANGEL has to stay in the prison all his life.");
49         else    printf("%d
",ans);
50     }
51     return 0;
52 }
53 inline bool Check(int x,int y){
54     return matrix[x][y]!='#'&&b[x][y]==0;
55 }
56 int Bfs(){
57     memset(b,0,sizeof(b));
58     while(!qu.empty())  qu.pop();
59     tmp.step=0;
60     tmp.x=x1;
61     tmp.y=y1_;
62     qu.push(tmp);
63     b[x1][y1_]=1;
64     while(!qu.empty()){
65         tmp=qu.top();
66         qu.pop();
67         int x=tmp.x,y=tmp.y;
68         int tx,ty,ts=tmp.step+1;
69         for(int i=0;i<4;i++){
70             tx=x+xy[0][i];
71             ty=y+xy[1][i];
72             if(Check(tx,ty)){
73                 if(matrix[tx][ty]=='.'){
74                     b[tx][ty]=1;
75                     tmp.x=tx;
76                     tmp.y=ty;
77                     tmp.step=ts;
78                     qu.push(tmp);
79                 }else if(matrix[tx][ty]=='x'){
80                     b[tx][ty]=1;
81                     tmp.x=tx;
82                     tmp.y=ty;
83                     tmp.step=ts+1;
84                     qu.push(tmp);
85                 }else{
86                     return ts;
87                 }
88             }
89         }
90     }
91     return -1;
92 }
View Code
原文地址:https://www.cnblogs.com/jiu0821/p/4354798.html