题目链接:http://poj.org/problem?id=2251

题目:

Description

You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of 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 containing three 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 describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank 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 5
S....
.###.
.##..
###.######
#####
##.##
##...#####
#####
#.###
####E1 3 3
S##
#E#
###0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!
题意:你处在一个三维的地牢里,从S出发逃到出口E,问最少要跑多远。

思路:这题虽然是一个三维的地图,但是做法和二维的没多大区别,不过要从当前层到其他层的要求是你所在位置为非#,且你将到的那层的这个位置也是非#。

代码实现如下:
 1 #include <queue>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5
 6 const int inf = 0x3f3f3f3f;
 7 int l, r, c, ans;
 8 int sx, sy, sz;
 9 char mp[35][35][35];
10 int vis[35][35][35];
11
12 struct node {
13     int x, y, z, step;
14 }nw, nxt;
15
16 int dx[6] = {1, -1, 0, 0, 0, 0}, dy[6] = {0, 0, 1, -1, 0, 0},
17     dz[6] = {0, 0, 0, 0, 1, -1};
18
19 void bfs(int z, int x, int y) {
20     vis[z][x][y] = 1;
21     nw.z = z, nw.x = x, nw.y = y, nw.step = 0;
22     queue<node> q;
23     q.push(nw);
24     while(!q.empty()) {
25         nw = q.front(), q.pop();
26         if(mp[nw.z][nw.x][nw.y] == 'E') {
27             ans = nw.step;
28             return;
29         }
30         for(int i = 0; i < 6; i++) {
31             nxt.z = nw.z + dz[i];
32             nxt.x = nw.x + dx[i];
33             nxt.y = nw.y + dy[i];
34             if(nxt.z >= 0 && nxt.z < l && nxt.x >= 0 && nxt.x < r && nxt.y >=0 && nxt.y < c && vis[nxt.z][nxt.x][nxt.y] == 0 && mp[nxt.z][nxt.x][nxt.y] != '#') {
35                 nxt.step = nw.step + 1;
36                 vis[nxt.z][nxt.x][nxt.y] = 1;
37                 q.push(nxt);
38             }
39         }
40     }
41 }
42
43 int main() {
44     while(~scanf("%d%d%d", &l, &r, &c) && (l + r + c)) {
45         for(int i = 0; i < l; i++) {
46             for(int j = 0; j < r; j++) {
47                 scanf("%s", mp[i][j]);
48                 for(int k = 0; k < c; k++) {
49                     if(mp[i][j][k] == 'S') {
50                         sx = j, sy = k, sz =i;
51                     }
52                 }
53             }
54         }
55         memset(vis, 0, sizeof(vis));
56         ans = inf;
57         bfs(sz, sx, sy);
58         if(ans >= inf) {
59             printf("Trapped!\n");
60         } else {
61             printf("Escaped in %d minute(s).\n", ans);
62         }
63     }
64     return 0;
65 }


转载于:https://www.cnblogs.com/Dillonh/p/8974741.html

Dungeon Master(三维bfs)相关推荐

  1. Dungeon Master 三维BFS

    三维BFS DungeonMasterDungeon\ MasterDungeon Master 跟二维没啥区别,就是方向多了几个,但在写的过程中出的bugbugbug还是蛮多的,所以记录一下吧! # ...

  2. poj 2251 Dungeon Master (三维bfs)

    http://poj.org/problem?id=2251 简单bfs,只不过是三维的... 唯一的坑点在输出上... Escaped in %d minute(s) 这意思是答案为1输出minut ...

  3. Dungeon Master题解bfs

    Dungeon Master 题面翻译 题目描述 输入格式 输出格式 样例 #1 样例输入 #1 样例输出 #1 题解思路及TIME EXCEEDED问题 题解 超时题解(个人觉得没问题) 题面翻译 ...

  4. (POJ - 2251)Dungeon Master(bfs)

    题目链接:2251 -- Dungeon Master (poj.org) 这是一个典型的bfs迷宫问题,只不过是三维的,唯一需要注意的就是输入要用cin,不要用scanf,因为有换行,其他没什么了, ...

  5. Dungeon Master(poj2251,bfs)

    http://poj.org/problem?id=2251 http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=15203 Dun ...

  6. Dungeon Master(bfs)广度优先搜索

    描述 You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of ...

  7. poj-2251 Dungeon Master【bfs】

    很基础的一道bfs /*这题是一个三维的迷宫题目,其中用'.'表示空地,'#'表示障碍物,'S'表示起点,'E'表示终点, 求从起点到终点的最小移动次数,解法和二维的类似, 只是在行动时除了东南西北移 ...

  8. pku 2251 Dungeon Master 基本BFS

    用了两种方式, 一种stl队列,一种自己实现的队列,事实证明stl就是好呀. stl万岁. #include <iostream> #include <queue> using ...

  9. POJ 2251 Dungeon Master(三维BFS求最短路径)

    3D dungeon 时间限制: 1 Sec  内存限制: 128 MB 提交: 2  解决: 2 [提交][状态][讨论版][命题人:201506020829][Edit] [TestData] 题 ...

  10. Dungeon Master(地牢大师、底下城主)三维bfs

    1253:Dungeon Master(地牢大师.底下城主) 要敢于变通 和普通bfs的区别,仅仅在于数组开三维+搜索方向多了上下(现在的搜索方向是 上下东南西北) bfs多组输入要记得清空队列 // ...

最新文章

  1. 一群工程师,让听障群体“看见”了声音
  2. python中二进制文件_Python学习基础篇 -6: Python中的文件操作
  3. android ContentObserver监听系统短信和备份短信到本地
  4. Handle/Body pattern(Wrapper pattern)
  5. ROS开发时的服务器启动命令
  6. JAVA类的无参方法
  7. SSH关于公钥认证Permission denied的问题
  8. hdu 1358 Period
  9. python和java选择哪个-python和Java选择哪一个?
  10. ftp服务器、文件夹中带点文件删除方法
  11. 百度和bing的背景肤色图片的保存
  12. php7和php5区别
  13. 系统项目验收测试报告怎么做?2020最新测试报告模板
  14. Introduction to SAP CPI
  15. 1501_FTA失效树分析简介
  16. 2020年中国电力线载波通信行业发展现状及竞争格局分析,国家电网持续推进电网转型升级,配电自动化覆盖率达到90%「图」
  17. 驾考 曲线行驶 s弯
  18. 相似度度量的不同方法
  19. iOS开发-舒尔特表
  20. 哈尔滨工业大学 计算机系教授,哈尔滨工业大学计算机科学与技术学院导师简介:张田文...

热门文章

  1. 优先队列 HDOJ 5437 Alisha's Party
  2. 农民思考互联网时代农民的未来
  3. cocos2d-x 中文 乱码问题
  4. No JDK found. Please validate either IDEA_JDK, JDK_HOME or JAVA_HOME
  5. lightgbm的GPU版本和CPU版本运行速度比较
  6. svm硬间隔与软间隔(转)
  7. mysql-workbench中用select in对变量赋值
  8. gitkraken同步建立repository与github上的repository
  9. ValueError: cannot index a corpus with zero features (you must specify either `num_features` or a no
  10. mysql_fetch_row ()出现段错误_207国道邵阳县段道路指示牌上出现错误,百姓疑惑”唐田市“在哪里...