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

Inputcopy Outputcopy
3 4 5
S....
.###.
.##..
###.# #####
#####
##.##
##... #####
#####
#.###
####E 1 3 3
S##
#E#
### 0 0 0

代码:

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int MAX=35;
int L,R,C;
char graph[MAX][MAX][MAX];//graph为地图
bool vis[MAX][MAX][MAX];//vis为标记
int beginx,beginy,beginz,endx,endy,endz;//分别记录起点和终点坐标
int dir[6][3]={{-1,0,0},{1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};//dir为6种走法
struct node{
int x,y,z;
int step;
};
void bfs(){
node d;
d.x=beginx,
d.y=beginy,
d.z=beginz;
d.step=0;
queue<node>q;
q.push(d);
vis[beginx][beginy][beginz]=true;
while(!q.empty()){
node n=q.front();
q.pop();
if(n.x==endx&&n.y==endy&&n.z==endz){
cout<<"Escaped in "<<n.step<<" minute(s)."<<endl;
return ;
}
for(int i=0;i<6;i++){
int x1=n.x+dir[i][0];
int y1=n.y+dir[i][1];
int z1=n.z+dir[i][2];
if(graph[x1][y1][z1]!='#'&&!vis[x1][y1][z1]&&x1>=0&&x1<L&&y1>=0&&y1<R&&z1>=0&&z1<C){
node n1;
n1.x=x1;
n1.y=y1;
n1.z=z1;
n1.step=n.step+1;
q.push(n1);
vis[x1][y1][z1]=true;
}
}
}
cout<<"Trapped!"<<endl;
}
int main(){
while(scanf("%d%d%d",&L,&R,&C)!=EOF){
memset(graph,'#',sizeof graph);
if(L==0&&R==0&&C==0)break;
for(int i=0;i<L;i++){
for(int j=0;j<R;j++){
cin>>graph[i][j];
for(int k=0;k<C;k++){
if(graph[i][j][k]=='S'){
beginx=i,beginy=j,beginz=k;
}
if(graph[i][j][k]=='E'){
endx=i,endy=j,endz=k;
}
}
}
}
memset(vis,0,sizeof vis);
bfs();
}
return 0;
}

POJ - 2251 地下城主的更多相关文章

  1. 洛谷 P2360 地下城主

    P2360 地下城主 题目描述 你参加了一项秘密任务,在任务过程中你被困在了一个3D的地下监狱里面,任务是计时的,你现在需要在最短的时间里面从地牢里面逃出来继续你的任务.地牢由若干层组成,每一层的形状 ...

  2. 【BFS】POJ 2251

    POJ 2251 Dungeon Master 题意:有一个地图,三维,走的方向是上下,左右,前后.问你最小步数从起始点走到出口. 思路:三维的BFS,就是多加一组状态,需要细心(不细心如我就找了半个 ...

  3. POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

    POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...

  4. POJ 2251 Dungeon Master(地牢大师)

    p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: ...

  5. 【POJ 2251】Dungeon Master(bfs)

    BUPT2017 wintertraining(16) #5 B POJ - 2251 题意 3维的地图,求从S到E的最短路径长度 题解 bfs 代码 #include <cstdio> ...

  6. POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索)

    POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索) Description You ar ...

  7. POJ.2251 Dungeon Master (三维BFS)

    POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...

  8. BFS POJ 2251 Dungeon Master

    题目传送门 /* BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 */ #include <cstdio> #includ ...

  9. [ACM训练] 算法初级 之 搜索算法 之 深度优先算法DFS (POJ 2251+2488+3083+3009+1321)

    对于深度优先算法,第一个直观的想法是只要是要求输出最短情况的详细步骤的题目基本上都要使用深度优先来解决.比较常见的题目类型比如寻路等,可以结合相关的经典算法进行分析. 常用步骤: 第一道题目:Dung ...

  10. poj 2251 Dungeon Master

    http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

随机推荐

  1. 如何基于 Agora Android SDK 在应用中实现视频通话?

    在很多产品,实时视频通话已经不是新鲜的功能了,例如视频会议.社交应用.在线教育,甚至也可能出现在一些元宇宙的场景中. 本文将教你如何通过声网Agora 视频 SDK 在 Android 端实现一个视频 ...

  2. Java 计算两个日期的时间间隔

    @Test public void durationTest(){ Temporal begin = LocalDateTime.of(2000,1,1,0,0); Temporal end = Lo ...

  3. 分布式缓存的一致性 Hash 算法

    一.使用一致性 Hash 算法的原因 简单的路由算法可以使用余数 Hash:用服务器数据除缓存数据 KEY 的 Hash 值,余数为服务器列表下标编码.这种算法可以满足大多数的缓存路由需求.但是,当分 ...

  4. Redis分布式Session和普通的cookie session有什么区别?

    Redis 是一种高性能的缓存和 key-value 存储系统,常被用来实现分布式 Session 的方案.在这种方案中,用户的登录信息存储在 Redis 中,而不是存储在本地的 cookie 或 s ...

  5. node.js介绍及简单例子

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. ML-程序练习-Dragon

    回归问题 前期 假设已有某样例,参数为w=1.477, b=0.089,即为\(y=1.477x+0.089\) 过程分析 数据采样 首先我们需要模拟一些带有真实样本观测误差的数据(因为真实情况是真实 ...

  7. 逍遥自在学C语言 | 变量、常量与数据类型

    前言 一.人物简介 第一位闪亮登场,有请今后会一直教我们C语言的老师 -- 自在. 第二位上场的是和我们一起学习的小白程序猿 -- 逍遥. 二.基本数据类型 1.整型 C语言中的整型数据如下表所示: ...

  8. 2023年php面试题

    Php面试题 1.isset和empty的区别? Isset测试变量是否被赋值,如果这个变量没被赋值,则返回false,empty是判断变量是否为空,当赋值为0,null,'',返回true为真.他们 ...

  9. 二进制安装Kubernetes(k8s) v1.24.1 IPv4/IPv6双栈

    二进制安装Kubernetes(k8s) v1.24.1 IPv4/IPv6双栈 Kubernetes 开源不易,帮忙点个star,谢谢了 介绍 kubernetes二进制安装 后续尽可能第一时间更新 ...

  10. day30:TCP&UDP:socket

    目录 1.TCP协议和UDP协议 2.什么是socket? 3.socket正文 1.TCP基本语法 2.TCP循环发消息 3.UDP基本语法 4.UDP循环发消息 4.黏包 5.解决黏包问题 1.解 ...