[数据结构]Angle迷宫(纯裸BFS)
2015-08-10 21:24
344 查看
/* Name:Angle迷宫(BFS) Actor:HT Time: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
相关文章推荐
- 数据结构实验:连通分量个数
- 【暑假】[实用数据结构] AC自动机
- 二叉树的基本功能实现
- 数据结构---二叉排序树(BST)
- 【暑假】[实用数据结构]KMP
- nginx高级数据结构源码分析(一)-----双向链表
- 数据结构(15)静态链表
- 数据结构(14)单链表
- 数据结构(13)线性表
- 【暑假】[实用数据结构]前缀树 Trie
- 可视化的数据结构和算法
- MySQL索引背后的数据结构及算法原理
- 数据结构(12)哈希表
- 数据结构(11)无向图相关算法基础
- 数据结构(9)平衡查找树之B树
- 数据结构(8)平衡查找树之红黑树
- 数据结构(7)平衡查找树之2-3树
- 数据结构(6)二叉树
- 数据结构(5)优先级队列与堆排序
- B-tree B+tree 数据结构解析