Rescue
来源:互联网 发布:淘宝上一张图片的大小 编辑:程序博客网 时间:2024/05/17 22:35
Rescue
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 42 Accepted Submission(s) : 8
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
#include<stdio.h>#include<string.h>#include<queue>#include<algorithm>using namespace std;int n, m, x, y, x1, Y;struct node{ int x, y, time; bool operator < (const node & a) const { return time > a.time; }}str, str1;priority_queue<node>Q;char a[205][205];int b[205][205]; // 标记走过的int fan[4][2] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } }; // 方向int BFS(int x, int y){ str.x = x; str.y = y; str.time = 0; Q.push(str); while (!Q.empty()) { str = Q.top(); Q.pop(); for (int i = 0; i < 4; i++) { if ((str.x + fan[i][0]) >= 0 && (str.x + fan[i][0])<n && (str.y + fan[i][1]) >= 0 && (str.y + fan[i][1])<m&&!b[str.x + fan[i][0]][str.y + fan[i][1]] && (a[str.x + fan[i][0]][str.y + fan[i][1]] != '#')) { if (a[str.x + fan[i][0]][str.y + fan[i][1]] == '.' || a[str.x + fan[i][0]][str.y + fan[i][1]] == 'a') { str1.x = str.x + fan[i][0]; str1.y = str.y + fan[i][1]; str1.time = str.time + 1; if (str1.x == x1&&str1.y == Y) return str1.time; Q.push(str1); b[str1.x][str1.y] = 1; } if (a[str.x + fan[i][0]][str.y + fan[i][1]] == 'x') { str1.x = str.x + fan[i][0]; str1.y = str.y + fan[i][1]; str1.time = str.time + 2; Q.push(str1); b[str1.x][str1.y] = 1; } } } } return 0;}int main(){ while (scanf("%d %d", &n, &m) != EOF) { int s = 0; x = y = x1 = Y = 0; for (int i = 0; i < n; i++) { scanf("%s", a[i]); for (int j = 0; j < m; j++) { if (a[i][j] == 'r') { x = i; y = j; } if (a[i][j] == 'a') { x1 = i; Y = j; } } } memset(b, 0, sizeof(b)); while (!Q.empty()) Q.pop(); b[x][y] = 1; s = BFS(x, y); if(s) printf("%d\n", s); else printf("Poor ANGEL has to stay in the prison all his life.\n"); } return 0;}
- Rescue
- Rescue
- Rescue
- Rescue
- Rescue
- Rescue
- Rescue
- Rescue
- Rescue
- Rescue
- Rescue
- Rescue
- Rescue
- Rescue
- Rescue
- Rescue
- Rescue
- Rescue
- 没可能, 南宁,
- 东源县副县长 就是东源的土皇帝
- freeSwitch安装步骤 for CentOS
- 新型的移动网页开发框架——Framework7(简介)
- 朴素Dijkstra算法模板
- Rescue
- 这是怎么了?
- 哥哥放假发给
- Java compiler level does not match the version of the installed Java project facet
- DeepLearning序列实例学习
- C/C++中的abort、atexit、exit和_Exit
- 基于注解实现springmvc的入门helloworld
- nodejs学习教程 nodejs express模块安装
- Lake Counting