码迷,mamicode.com
首页 > 其他好文 > 详细

hdu1242 优先队列+bfs

时间:2015-09-17 23:19:05      阅读:387      评论:0      收藏:0      [点我收藏+]

标签:

Rescue

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


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:  1240 1016 1010 1072 1241 
技术分享
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <queue>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 struct Point
 8 {
 9     int x,y,t;
10     bool operator < (const Point &a)const
11     {
12         return t>a.t;
13     }
14 };
15 
16 char map[205][205];
17 Point start;
18 int n,m;
19 int diect[][2]={{1,0},{-1,0},{0,1},{0,-1}}; 
20 
21 int bfs()
22 {    
23     priority_queue<Point> que;
24     Point cur,next;
25     int i,j;
26 
27     map[start.x][start.y]=#;
28     que.push(start);
29     while(!que.empty())
30     {
31         cur=que.top();
32         que.pop();
33         for(i=0;i<4;i++)
34         {
35             next.x=cur.x+diect[i][0];  
36             next.y=cur.y+diect[i][1];  
37             next.t=cur.t+1;
38             if(next.x<0 || next.x>=n || next.y<0 || next.y>=m)
39                 continue;
40             if(map[next.x][next.y]==#)
41                 continue;
42             if(map[next.x][next.y]==r)
43                 return next.t;
44             if(map[next.x][next.y]==.)
45             {
46                 map[next.x][next.y]=#;
47                 que.push(next);
48             }
49             else if(map[next.x][next.y]==x)
50             {
51                 map[next.x][next.y]=#;
52                 next.t++;
53                 que.push(next);
54             }
55         }
56     }
57     return -1;
58 }
59 
60 int main()
61 {
62     int i,j,ans;
63     char *p;
64     while(scanf("%d %d",&n,&m)!=EOF)
65     {
66         for(i=0;i<n;i++)
67         {
68             scanf("%s",map[i]);
69             if(p=strchr(map[i],a))
70             {
71                 start.x=i;
72                 start.y=p-map[i];
73                 start.t=0;
74             }
75         }
76         ans=bfs();
77         if(ans==-1)
78             printf("Poor ANGEL has to stay in the prison all his life.\n");
79         else
80             printf("%d\n",ans);
81     }
82     return 0;
83 }
View Code

 

hdu1242 优先队列+bfs

标签:

原文地址:http://www.cnblogs.com/cyd308/p/4817740.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!