Dungeon Master-BFS
来源:互联网 发布:淘宝店铺评价好的包包 编辑:程序博客网 时间:2024/06/05 07:00
Submit
Status
Practice
POJ 2251
Description
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composedof unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides. Is an escape possible? If yes, how long will it take?
Input
The input consists of a number of dungeons. Each dungeon description starts with a line containingthree integers L, R and C (all limited to 30 in size). L is the number of levels making up the dungeon. R and C are the number of rows and columns making up the plan of each level. Then there will follow L blocks of R lines each containing C characters. Each character describesone cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are representedby a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a singleblank line after each level. Input is terminated by three zeroes for L, R and C.
Output
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form Escaped in x minute(s).where x is replaced by the shortest time it takes to escape. If it is not possible to escape, print the line Trapped!
Sample Input
3 4 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0
Sample Output
Escaped in 11 minute(s).Trapped!
代码
#include <iostream>#include <queue>#include <cstring>#include <cstdio>using namespace std;char c[50][50][50];int vis[50][50][50]; //对访问过的点进行标记 int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; int i, k, j, x, y, n, m, sx, sy,sz; //用sx,sy,sz来表示起点S的三维坐标 struct T { //x,y,z表示任意点的坐标,cnt表示到达此点时的步数 int z; int x; int y; int cnt;}now, eed;int can(struct T a) {//约束条件 if(c[a.z][a.x][a.y] == '#' || a.z <= 0 || a.z > k || a.x <= 0 || a.x > n || a.y <= 0 || a.y > m || vis[a.z][a.x][a.y]) return 0; return 1;}int bfs()//广度优先搜索 { queue<T> maze; memset(vis, 0, sizeof(vis)); now.z = sz; now.x = sx; now.y = sy; now.cnt = 0; maze.push(now); vis[now.z][now.x][now.y] = 1; while( !maze.empty()) { now = maze.front(); if(c[now.z][now.x][now.y] == 'E') return now.cnt; //找到终点,返回终点的cnt值 eed.cnt = now.cnt + 1; for(i = 0; i < 4; i++) { //二维平面内向四个方向搜索, 即north, south, east, west eed.z = now.z; eed.x = now.x + dir[i][0]; eed.y = now.y + dir[i][1]; if(can(eed))//如果符合条件,就放到队列中 { maze.push(eed); vis[eed.z][eed.x][eed.y] = 1; } } //二维平面的上方,即up eed.z = now.z + 1; eed.x = now.x; eed.y = now.y; if(can(eed)) { maze.push(eed); vis[eed.z][eed.x][eed.y] = 1; } //二维平面的下方,即down eed.z = now.z - 1; eed.x = now.x; eed.y = now.y; if(can(eed)) { maze.push(eed); vis[eed.z][eed.x][eed.y] = 1; } maze.pop(); } return -1;}int main(){ while(scanf("%d %d %d", &k, &n, &m) && k+n+m != 0) { for(int i = 1; i <= k; i++) { for(int j = 1; j <= n; j++) { for(int a = 1; a <= m; a++) { cin >> c[i][j][a]; if(c[i][j][a] == 'S') { sz = i; sx = j; sy = a; } } } } int result = bfs(); if(result == -1) printf("Trapped!\n"); else printf("Escaped in %d minute(s).\n", result); } return 0;}
阅读全文
0 0
- poj2251 Dungeon Master (BFS)
- (BFS)2251 Dungeon Master
- Dungeon Master bfs
- 532 - Dungeon Master//bfs
- Dungeon Master(poj2251,bfs)
- Dungeon Master(bfs)
- Dungeon Master BFS
- POJ2251:Dungeon Master(BFS)
- poj2251 Dungeon Master bfs
- Dungeon Master uva BFS
- POJ2251:Dungeon Master(BFS)
- toj1140. Dungeon Master (bfs)
- poj2251 Dungeon Master(BFS)
- POJ2251 Dungeon Master BFS
- Dungeon Master-BFS
- [bfs]poj2251 Dungeon Master
- poj2251 dungeon master【BFS】~
- 2251 Dungeon Master【bfs】
- 《Java虚拟机》必知必会——十四个问题总结(内存模型+GC)
- 链表的二路归并排序
- NodeJS 使用express、mongolian搭建轻量级web应用
- 重构零售与网商!马云集阿里力量建设“五新”
- Less 和 Sass 及其开发环境
- Dungeon Master-BFS
- 从零开始nodejs系列文章
- 2016-9月份至今读的小说记录
- @NotEmpty、@NotBlank、@NotNull的区别
- 解决fatal: unable to connect to github.com问题
- 如果ngxin不能使用service nginx start 开启
- 23种设计模式(9)-外观模式
- npm 配置
- Angular中$compile源码分析