hdu 1242 Rescue(bfs)

Rescue

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


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
 
Recommend
Eddy   |   We have carefully selected several similar problems for you:  1072 1372 1180 1401 1239 
 
因为有些点需要走两秒,所以路程最短的路不一定是时间最短,所以需要使用优先队列,实际为优先队列的模板题。
 
题意:有一名天使被囚禁了,天使的朋友要去救他,你的任务是输出最短的救援时间。天使的朋友每秒可以走一步,地牢中有守卫,当你遇到守卫的时候需要停留一秒杀死守卫。给你地牢的地图,上面有几种元素,'.'表示路,可以通行。'#'代表墙,无法通行。'r'表示天使的朋友,代表起点。'a'表示天使的位置,代表终点。'x'表示守卫的位置。
 
附上代码:
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <queue>
 5 using namespace std;
 6 
 7 const int M=205;
 8 char map[M][M];
 9 int n,m,visit[M][M];
10 int f[4][2]= {0,1,0,-1,1,0,-1,0};   //模拟四个方向的变化
11 int a,b;
12 struct node
13 {
14     int x,y,t;
15     friend bool operator < (node n1,node n2)  //优先队列,按时间从小到大排列
16     {
17         return n1.t>n2.t;
18     }
19 } s1,s2;
20 
21 void BFS()
22 {
23     priority_queue <node> q;  //优先队列的写法
24     while(!q.empty())
25         q.pop();
26     s1.x=a;
27     s1.y=b;
28     s1.t=0;
29     visit[a][b]=1;   //起点标记为走过
30     q.push(s1);
31     while(!q.empty())
32     {
33         s1=q.top();       //与普通队列的不同
34         q.pop();
35         if(map[s1.x][s1.y]=='a')   //结束标志找到了天使
36         {
37             printf("%d
",s1.t);
38             return;
39         }
40         for(int i=0; i<4; i++)
41         {
42             s2.x=s1.x+f[i][0];
43             s2.y=s1.y+f[i][1];
44             if(s2.x>=0&&s2.x<n&&s2.y>=0&&s2.y<m&&!visit[s2.x][s2.y]&&map[s2.x][s2.y]!='#')  //剪枝条件
45             {
46                 visit[s2.x][s2.y]=1;   //走过的路标记为1
47                 s2.t=s1.t+1;
48                 if(map[s2.x][s2.y]=='x')  //若遇到‘x’,则需要两秒的时间
49                     s2.t=s1.t+2;
50                 q.push(s2);
51             }
52         }
53     }
54     cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;  //找不到的输出
55     return;
56 }
57 
58 int main()
59 {
60     int i,j;
61     while(~scanf("%d%d",&n,&m))
62     {
63         for(i=0; i<n; i++)
64             for(j=0; j<m; j++)
65             {
66                 cin>>map[i][j];
67                 if(map[i][j]=='r')  //记录起点的位置
68                 {
69                     a=i;
70                     b=j;
71                 }
72             }
73         memset(visit,0,sizeof(visit));
74         BFS();
75     }
76     return 0;
77 }
原文地址:https://www.cnblogs.com/pshw/p/4754773.html