题目链接:点击打开链接

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!

大意:3维的迷宫。从S起点到E终点的最短时间。如果不能到达,就输出那句话。

思路:涉及最短步数一般都是BFS,注意细节就行了。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<map>
#include<vector>
#include<string>
#include<set>
#include<queue>
using namespace std;
const int MAX = 40;char G[MAX][MAX][MAX];
int vis[MAX][MAX][MAX];int l, n, m, flag;struct node{int x;int y;int z;int loyer;
}Node, S, E, temp;//Node topnode S E temp可以设置这些变量int dir[6][3] = {0, -1, 0, 0, 1, 0, 1, 0, 0, -1, 0, 0, 0, 0, -1, 0, 0, 1};//6个方向bool judge(int x, int y, int z) {//判断这个点if(x < 0 || x >= n || y < 0 || y >= m || z < 0 || z >= l)//出界(注意是>= <=)return false;if(vis[x][y][z] || G[x][y][z] == '#')return false;return true;
}int bfs(int x, int y, int z) {//bfs找终点queue<node> q;Node.x = x;Node.y = y;Node.z = z;Node.loyer = 0;q.push(Node);//压起点vis[x][y][z] = 1;//标记while(!q.empty()) {node topnode = q.front();//取出一个q.pop();//记得删除temp.loyer = topnode.loyer + 1;//先加步数for(int i = 0; i < 6; i++) {//6个方向访问temp.x = topnode.x + dir[i][0];temp.y = topnode.y + dir[i][1];temp.z = topnode.z + dir[i][2];if(judge(temp.x, temp.y, temp.z)) {//是否到终点if(G[temp.x][temp.y][temp.z] == 'E')return temp.loyer;//返回步数q.push(temp);vis[temp.x][temp.y][temp.z] = 1;//标记}}}return -1;
}int main() {while(~scanf("%d %d %d", &l, &n, &m)) {if(l == 0 && n == 0 && m == 0)break;memset(vis, 0, sizeof(vis));for(int k = 0; k < l; k++) {for(int i = 0; i < n; i++) {for(int j = 0; j < m; j++) {cin >> G[i][j][k];//不要用scanf了。难受if(G[i][j][k] == 'S') {S.x = i;S.y = j;S.z = k;}}}}flag = bfs(S.x, S.y, S.z);if(flag != -1)printf("Escaped in %d minute(s).\n", flag);elseprintf("Trapped!\n");}
}

转载于:https://www.cnblogs.com/ACMerszl/p/9572984.html

POJ2251-Dungeon Master相关推荐

  1. POJ-2251 Dungeon Master

    POJ-2251 Dungeon Master 题目 You are trapped in a 3D dungeon and need to find the quickest way out! Th ...

  2. POJ-2251 Dungeon Master

    这道题呢其实就是一个迷宫问题,只不过从二维变成了三维而已 首先呢这道题是用宽搜做的,可以从其数据范围看出 我本人一开始用深搜做,然后一直超时,看了数据范围以后才用的宽搜(其实最短路时是最好用宽搜的) ...

  3. POJ-2251 Dungeon Master bfs搜索

    就是将普通的二维推广到三维,方向变成了六个. 代码如下: #include <cstring> #include <cstdio> #include <cstdlib&g ...

  4. poj-2251 Dungeon Master【bfs】

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

  5. 【POJ2251】Dungeon Master(三维BFS)

    题面:[POJ2251]Dungeon Master 这是一道很有意思的题目,平时我们所接触的BFS大多是二维的,可这题不一样,这题是一道三维BFS!!! 这题不需要多讲,直接上代码就可以了: #in ...

  6. Dungeon Master(poj2251,bfs)

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

  7. BFS - Dungeon Master

    Dungeon Master 题目链接: https://vjudge.net/problem/POJ-2251 题目: You are trapped in a 3D dungeon and nee ...

  8. B - Dungeon Master POJ - 2251

    B - Dungeon Master POJ - 2251 题目: 逃离3D迷宫,n <=30 首先是dfs,算一下最坏情况共30层,每层有30*30的循环, 复杂度 3030*30 显然严重爆 ...

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

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

  10. 信息学奥赛一本通(1248:Dungeon Master)

    1248:Dungeon Master 时间限制: 1000 ms         内存限制: 65536 KB 提交数: 8637     通过数: 3432 [题目描述] 这题是一个三维的迷宫题目 ...

最新文章

  1. 《Web安全之机器学习入门》一 2.2 TensorFlow简介与环境搭建
  2. routeros v6.43.2_routeros软路由负载均衡,充分利用带宽,提高效率降低成本
  3. (asp.net MVC学习)System.Web.Mvc.UrlHelper的学习与使用
  4. MySQL数据库性能优化的八种方式
  5. CUDA与Java速度比较---生成Julia数据集并画图
  6. linux查文件名称唯美,第二章、Linux常用命令
  7. 移动网页如何实现发送短信和拨打电话的功能
  8. [Swift]LeetCode1013. 将数组分成和相等的三个部分 | Partition Array Into Three Parts With Equal Sum...
  9. php 生成器作用,php 生成器的理解和使用
  10. php嗅探木马,PHP安全-密码嗅探
  11. erlang 程序设计书中的错误
  12. windows理论基础(一)
  13. vue cli3--创建通用模版
  14. log4j 配置和使用
  15. 看看我能帮上大家的什么忙? 文平
  16. cap 2 加州房价预测
  17. Python小游戏:猜大小
  18. 5.22 综合案例2.0-4G远程遥控车DEMO(2.2版本接口有更新)
  19. 【树莓派不吃灰】基础篇① 半小时搭建树莓派3B可运行环境(不需要显示器,不需要网线)
  20. 【UML】——活动图

热门文章

  1. 【C语言编程】 大小写字母互换
  2. 图像中的天空区域检测!
  3. LED驱动电源有几种保护方式
  4. 【原来python还可以这么玩】python逆向爬取网易云评论进行情感分析
  5. 找回生命本源的力量,走上生命觉醒的旅程
  6. 泡沫经济中的泡沫游戏
  7. 一种4Gwifi远程控制三色灯蜂鸣报警器支持ERP安灯系统MESAPS对接
  8. 前、后台网站模板参考
  9. 读书笔记之JVM垃圾回收
  10. WinFrom内嵌chrome浏览器