Rescue
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
算法分析:本题使用一个mintime[x][y]二维数组来表示起点到图中(x,y)位置处的最短时间,由于最短路径中的某一个方案所得出的结果不一定最优解,所以一定要将所有的方案都搜到之后,才能输出结果。对于每一个节点,只扩展到下一个节点位置(相邻的4个方向)处所用的时间比mintime在该位置所用时间少的。BFS完成之后直接输出mintime[end_x][end_y]即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
#include <iostream> #include <fstream> #include <cstring> #include <queue> using namespace std; const int maxn = 210 + 36; const int INF = 66666666; const int dx[] = {0, 0, -1, 1}; const int dy[] = {-1, 1, 0, 0}; struct Node { int x, y, t; Node (int xx, int yy, int tt) : x (xx), y (yy), t (tt) {} }; char ans[maxn][maxn]; int mintime[maxn][maxn]; int row, col, s_x, s_y, e_x, e_y; inline bool OkMove (int x, int y) { if (x >= 0 && x <= row - 1 && y >= 0 && y <= col - 1 && ans[x][y] != '#') return true; return false; } void BFS (int x, int y, int t) { queue <Node> q; q.push (Node (x, y, t)); mintime[x][y] = 0; while (!q.empty ()) { Node temp = q.front (); q.pop (); int i; for (i = 0; i < 4; i++) { if (OkMove (temp.x + dx[i], temp.y + dy[i])) { Node val (temp.x + dx[i], temp.y + dy[i], temp.t + 1); if (ans[val.x][val.y] == 'x') val.t++; if (mintime[val.x][val.y] > val.t) { mintime[val.x][val.y] = val.t; q.push (Node (val.x, val.y, val.t)); } } } } } int main() { // ifstream cin ("aaa.txt"); while (cin >> row >> col) { int i, j; for (i = 0; i < row; i++) for (j = 0; j < col; j++) { cin >> ans[i][j]; mintime[i][j] = INF; if (ans[i][j] == 'a') { s_x = i; s_y = j; } else if (ans[i][j] == 'r') { e_x = i; e_y = j; } } BFS (s_x, s_y, 0); if (mintime[e_x][e_y] == INF) cout << "Poor ANGEL has to stay in the prison all his life." << endl; else cout << mintime[e_x][e_y] << endl; } return 0; } |
- « 上一篇:zoj1403
- zoj1718:下一篇 »