[数据结构]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
- [数据结构]Angle迷宫(纯裸BFS)
- 数据结构 迷宫问题逐渐形成的报告(bfs、栈)
- 逃离迷宫(bfs)
- 迷宫问题(BFS)
- 迷宫问题(bfs)
- 计蒜客 迷宫 (bfs)
- 迷宫问题(BFS)
- 逃离迷宫(BFS)
- 复习数据结构---纯C编译栈及栈应用(迷宫)
- Codevs 1215 迷宫 裸BFS
- poj3984(BFS)迷宫路径
- HDU1728逃离迷宫(BFS)
- SDJZUOJ迷宫问题(BFS)
- sdjzu1102 迷宫问题(BFS)
- poj3984 迷宫问题(BFS)
- POJ3984 迷宫问题(BFS)
- NYOJ82 迷宫寻宝(BFS)
- UVa11624 Fire!(BFS+迷宫)
- 数据同步平台——FTP
- 找不到lua_open
- 续之前那个图书管理代码,还在修改实现中
- HDU 4169 Wealthy Family 优先队列
- 微软创投加速器最新成果展示:人工智能技术杀入时尚界
- [数据结构]Angle迷宫(纯裸BFS)
- LCS
- SQL中的and和or
- 点语法
- 浅谈OFBiz之权限设计
- 功夫。
- 【ACM] HDU 5025 Saving Tang Monk (状态压缩,BFS)
- 【Dubbo实战】基础学习篇(一)
- OC之NSString