[数据结构]Angle迷宫(纯裸BFS)

来源:互联网 发布:苹果看视频软件 编辑:程序博客网 时间:2024/05/16 19:26
/*Name:Angle迷宫(BFS)Actor:HTTime:2015年8月7日Error Reporte:1.时间的计数上要注意}*/#include <iostream>#include <stdio.h>#include <conio.h>#include <string.h>#include <algorithm>using namespace std;char map[205][205];int ma[4] = { 1, -1, 0, 0 };int mb[4] = { 0, 0, 1, -1 };int m, n;int hx, hy;int front, rear;int qx[205];int qy[205];int time[205][205];int bfs(){int i, j;front = 0;rear = 1;qx[front] = hx;qy[front] = hy;int mx, my;memset(time, 0, sizeof(time));while (front < rear){for (i = 0; i < 4; i++){mx = qx[front] + ma[i];my = qy[front] + mb[i];if (mx >= 0 && my >= 0 && mx < m&&my < n&&map[mx][my] != '#'){if (map[mx][my] == '.'){map[mx][my] = '#';time[mx][my] += time[qx[front]][qy[front]] + 1;qx[rear] = mx;qy[rear] = my;rear++;}else if (map[mx][my] == 'x'){map[mx][my] = '.';time[mx][my] += 1;//错误点,时间增长,注意qx[rear] = qx[front];qy[rear] = qy[front];rear++;}else if (map[mx][my] == 'r'){map[mx][my] = '#';time[mx][my] += time[qx[front]][qy[front]] + 1;return time[mx][my];}}}front++;}return -1;}int main(){int i, j;while (scanf("%d %d",&m, &n) != EOF){memset(map, 0, sizeof(map));hx = hy = -1;for (i = 0; i < m; i++){cin >> map[i];if (hx != -1 && hy != -1) continue;for (j = 0; j < n; j++){if (map[i][j] == 'a'){hx = i; hy = j;break;}}}int t=bfs();/*for (i = 0; i < m; i++){for (j = 0; j < n; j++){printf("%d ", time[i][j]);}cout << endl;}*/if (t == -1)printf("Poor ANGEL has to stay in the prison all his life.\n" );else printf("%d\n", t);}return 0;}


----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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



0 0