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

杭电1242--Rescue(BFS+优先队列)

时间:2015-07-30 21:03:40      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:

Rescue

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


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 1253 
//第一道这样的题,思路大致了解 。
 1 #include <queue>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 using namespace std;
 6 
 7 
 8 int dir[4][2]={-1, 0, 1, 0, 0, -1, 0, 1};
 9 char map[220][220];  int vis[220][220]; 
10 int i, j, m, n;
11 
12 struct prision
13 {
14     int x, y, time;
15     friend bool operator < (prision x, prision y)
16     {
17         return x.time > y.time;      //从小  →  大; 
18     }
19 } current, next;
20 
21 
22 int BFS(int x,int y)
23 {
24 
25 
26     priority_queue <prision>q;
27     prision current,next;
28     memset(vis,0,sizeof(vis));
29 
30     current.x=x;
31     current.y=y;
32     current.time=0;
33     vis[current.x][current.y]=1;
34     q.push(current);
35 
36 
37     while(!q.empty())
38     {
39 
40         current=q.top();
41         q.pop();
42         for(int i=0;i<4;i++)
43         {
44             next.x=current.x+dir[i][0];
45             next.y=current.y+dir[i][1];
46 
47             if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&map[next.x][next.y]!=#&&!vis[next.x][next.y])
48             {
49 
50                 if(map[next.x][next.y]==r)
51                     return current.time+1;
52                 
53                 if(map[next.x][next.y]==x)
54                     next.time=current.time+2;
55                 else
56                     next.time=current.time+1;
57                 vis[next.x][next.y]=1;
58                 q.push(next);
59             }
60         }
61     }
62     return -1;
63 } 
64 
65 
66 int main()
67 {
68     prision angle;
69     while(~scanf("%d %d", &n, &m))
70     {
71         for(i=0; i<n; i++)
72         {
73             for(j=0; j<m; j++)
74             {
75                 cin >> map[i][j];
76                 if(map[i][j] == a)
77                 {
78                     angle.x = i;
79                     angle.y = j;
80                 }
81             }
82         }
83         int time = BFS(angle.x, angle.y);
84         
85         if(time == -1)
86             cout <<"Poor ANGEL has to stay in the prison all his life."<<endl;
87         else
88             cout <<time<< endl;  
89     }
90     return 0;
91 }  

 

 
 
 

杭电1242--Rescue(BFS+优先队列)

标签:

原文地址:http://www.cnblogs.com/fengshun/p/4690331.html

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