HDU 1242 Rescue(优先队列)

题目来源:

http://acm.hdu.edu.cn/showproblem.php?pid=1242

题目描述:

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
 
题意描述:
有多个‘r’和一个‘a’,问从r走向a最短时间是多少,其中遇到x表示守卫,需要多加一个单位时间。
解题思路:
刚上来以为是最短路,用了BFS来做,后来发现,BFS找到的是最短步数,与最短时间是不一样的,也就是说,最短步数的时间不一定是最短时间。最后使用优先队列,出队的时候保证该点的时间时最短的即可。
代码实现:
 1 #include<stdio.h>
 2 #include<queue>
 3 using namespace std;
 4 
 5 int r,c;
 6 struct point {
 7     int x,y,s;
 8     bool operator < (const point &a) const
 9     {
10         return a.s<s;
11     }
12 };
13 
14 char map[210][210];
15 int bfs(int sx,int sy);
16 
17 int main()
18 {
19     int i,j,sx,sy;
20     while(scanf("%d%d",&r,&c) != EOF)
21     {
22         for(i=1;i<=r;i++)
23             for(j=1;j<=c;j++)
24             {
25                 scanf(" %c",&map[i][j]);
26                 if(map[i][j]=='a')
27                 {
28                     sx=i;sy=j;
29                 }
30             }
31             
32         int ans=bfs(sx,sy);
33         
34         if(!ans)
35         printf("Poor ANGEL has to stay in the prison all his life.\n");
36         else    
37         printf("%d\n",ans); 
38     }
39     return 0;
40 } 
41 int bfs(int sx,int sy)
42 {
43     int next[4][2]={0,1,1,0,0,-1,-1,0};
44     priority_queue<struct point>q;
45     struct point cur,nex;
46     int k,tx,ty;
47     
48     cur.x=sx;
49     cur.y=sy;
50     cur.s=0;
51     q.push(cur);
52     map[sx][sy]='#';
53     
54     while(!q.empty())
55     {
56         cur=q.top();
57         for(k=0;k<=3;k++)
58         {
59             tx=cur.x+next[k][0];
60             ty=cur.y+next[k][1];
61             if(tx < 1 || tx > r || ty < 1 || ty > c) 
62                 continue;
63             if(map[tx][ty] != '#')
64             {
65                 nex.x=tx;
66                 nex.y=ty;
67                 if(map[tx][ty]=='x')
68                     nex.s=cur.s+2;    
69                 if(map[tx][ty]=='.')
70                     nex.s=cur.s+1;    
71                 if(map[tx][ty]=='r')
72                     return cur.s+1;
73                 q.push(nex);
74                 map[tx][ty]='#';
75             }
76         }
77         q.pop();
78     }
79     return 0; 
80 }

易错分析:

优先队列还是直接使用C++的模板好,自己写的容易出问题

 
 
 

 

posted @ 2018-01-24 22:46  Reqaw  阅读(235)  评论(0编辑  收藏  举报