HDU 1242 Rescue
来源:互联网 发布:面料常见花纹知乎 编辑:程序博客网 时间:2024/05/21 19:03
Rescue
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.)
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.
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。
#include <bits/stdc++.h>#define endl "\n"using namespace std;typedef long long ll;const int MAXN = 200 + 7;int n, m;int dx[] = {0, -1, 0, 1};int dy[] = {1, 0, -1, 0};int vis[MAXN][MAXN];char gra[MAXN][MAXN];bool isValid(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m;}struct Point { Point() {} Point(int x,int y, int pri) : x(x), y(y), pri(pri) {} int x, y; int pri; // 优先级, 按照值小的优先级高排序 bool operator < (const Point &p) const { return pri > p.pri; }};int bfs(int x, int y) { vis[x][y] = 1; priority_queue<Point> q; q.push(Point(x, y, 0)); while(!q.empty()) { Point p = q.top(); q.pop(); int pri = p.pri; for(int i = 0; i < 4; ++i) { int xx = p.x + dx[i], yy = p.y + dy[i]; if(isValid(xx, yy) && !vis[xx][yy] && gra[xx][yy] == '.') { vis[xx][yy] = 1; q.push(Point(xx, yy, pri + 1)); } else if(isValid(xx, yy) && !vis[xx][yy] && gra[xx][yy] == 'x') { vis[xx][yy] = 1; q.push(Point(xx, yy, pri + 2)); } else if(isValid(xx, yy) && gra[xx][yy] == 'a') {return pri+1;} } } return 0;}int main() { ios::sync_with_stdio(false); while(cin >> n >> m) { memset(vis, 0, sizeof(vis)); for(int i = 0; i < n; ++i) { for(int j = 0; j < m; ++j) { cin >> gra[i][j]; } } int ans = 0; for(int i = 0; i < n; ++i) { for(int j = 0; j < m; ++j) { if(gra[i][j] == 'r') { ans = bfs(i, j); break; } } } if(ans) cout << ans << endl; else cout << "Poor ANGEL has to stay in the prison all his life." << endl; } return 0;}
0 0
- hdu 1242 Rescue
- HDU-1242-Rescue
- HDU 1242 Rescue
- HDU-1242 Rescue
- hdu 1242 Rescue
- hdu 1242 Rescue<java>
- hdu 1242 rescue
- HDU-1242:Rescue
- HDU 1242 Rescue
- HDU 1242 Rescue
- hdu 1242 Rescue
- hdu 1242 Rescue
- hdu 1242 Rescue
- hdu 1242 Rescue
- hdu 1242 Rescue
- Rescue hdu 1242
- hdu-1242-Rescue
- Hdu 1242 Rescue
- 190行LUA撸撸撸了一天。
- 四则运算计算器(面向对象编程思维)
- 226. Invert Binary Tree
- 暑期实习面试总结
- 查看mysql安装路径
- HDU 1242 Rescue
- C#上机实验之题目一
- 第一天
- 六种姿势拿下连续子序列最大和问题,附伪代码(以HDU 1003 1231为例)
- POJ 2533 Longest Ordered Subsequence
- poj1260
- 胸部肌肉锻炼
- 如何台灣駕照換深圳駕照
- 大端小端存储模式