首页 > 其他 > 详细

hdu 1242 Rescue

时间:2015-06-28 14:06:53      阅读:105      评论:0      收藏:0      [点我收藏+]

题目连接

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

Rescue

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

bfs+优先队列。。

技术分享
 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<vector>
 7 #include<queue>
 8 #include<map>
 9 using std::map;
10 using std::cin;
11 using std::cout;
12 using std::endl;
13 using std::find;
14 using std::sort;
15 using std::pair;
16 using std::vector;
17 using std::priority_queue;
18 #define pb(e) push_back(e)
19 #define sz(c) (int)(c).size()
20 #define mp(a, b) make_pair(a, b)
21 #define all(c) (c).begin(), (c).end()
22 #define iter(c) decltype((c).begin())
23 #define cls(arr,val) memset(arr,val,sizeof(arr))
24 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
25 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
26 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
27 const int Max_N =210;
28 typedef unsigned long long ull;
29 const int dx[] = { 0, 0, 1, -1 }, dy[] = { 1, -1, 0, 0 };
30 bool vis[Max_N][Max_N];
31 char maze[Max_N][Max_N];
32 int N, M, Sx, Sy, Dx, Dy;
33 struct Node {
34     int x, y, s;
35     Node(int i = 0, int j = 0, int k = 0) :x(i), y(j), s(k) {}
36     bool operator<(const Node &a) const {
37         return s > a.s;
38     }
39 };
40 void bfs() {
41     cls(vis, false);
42     priority_queue<Node> que;
43     que.push(Node(Sx, Sy, 0));
44     vis[Sx][Sy] = true;
45     while (!que.empty()) {
46         Node tp = que.top(); que.pop();
47         if (tp.x == Dx && tp.y == Dy) { printf("%d\n", tp.s); return; }
48         rep(i, 4) {
49             int nx = dx[i] + tp.x, ny = dy[i] + tp.y;
50             if (nx < 0 || nx >= N || ny < 0 || ny >= M) continue;
51             if (maze[nx][ny] == # || vis[nx][ny]) continue;
52             if (maze[nx][ny] == x) que.push(Node(nx, ny, tp.s + 2));
53             else que.push(Node(nx, ny, tp.s + 1));
54             vis[nx][ny] = true;
55         }
56     }
57     puts("Poor ANGEL has to stay in the prison all his life.");
58 }
59 int main() {
60 #ifdef LOCAL
61     freopen("in.txt", "r", stdin);
62     freopen("out.txt", "w+", stdout);
63 #endif
64     while (~scanf("%d %d", &N, &M)) {
65         rep(i, N) {
66             scanf("%s", maze[i]);
67             rep(j, M) {
68                 if (maze[i][j] == a) Dx = i, Dy = j;
69                 else if (maze[i][j] == r) Sx = i, Sy = j;
70             }
71         }
72         bfs();
73      }
74     return 0;
75 }
View Code

hdu 1242 Rescue

原文:http://www.cnblogs.com/GadyPu/p/4605411.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!