Dungeon Master

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....
.###.
.##..
###.# #####
#####
##.##
##... #####
#####
#.###
####E 1 3 3
S##
#E#
### 0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring> using namespace std;
const int inf=-1;
struct TP
{
int h,x,y;
TP(int h, int x, int y):h(h), x(x), y(y) {}
};
int sh,sx,sy,gh,gx,gy;
char a[100][100][100];
int m=0,n=0,h=0;
int d[100][100][100];
int dh[6]= {0,0,0,0,1,-1},dx[6]= {1,0,-1,0,0,0},dy[6]= {0,1,0,-1,0,0};
int dfs()
{
queue<TP> que;
for(int k=0; k<h; k++)
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
d[k][i][j]=inf;//初始化所有距离标记
que.push(TP(sh,sx,sy));//把开始加入队列
d[sh][sx][sy]=0;//重新定义开头的距离
while(!que.empty())//队列的循环
{ TP p = que.front();//使用队列的开头
que.pop();//踢出
if(p.h==gh&&p.x==gx&&p.y==gy) break;//判断是否是结尾
for(int i=0; i<6; i++)//移动
{
int nx=p.x+dx[i],ny=p.y+dy[i],nh=p.h+dh[i];
if(0 <= nh && 0 <= nx && 0 <= ny && nx < n && ny < m&& nh < h && a[nh][nx][ny]!='#'&&d[nh][nx][ny]==inf)//判断是否是边界以及栅栏
{
que.push(TP(nh,nx,ny));//把移动后的加入队列
d[nh][nx][ny]=d[p.h][p.x][p.y]+1;
}
}
}
return d[gh][gx][gy];
}
int main()
{
while(scanf("%d%d%d",&h,&n,&m)!=-1)
{
if(h==0&&n==0&&m==0)
return 0;
getchar();
for(int k=0; k<h; k++)
{
for(int i=0; i<n; i++)//
{
scanf("%s",&a[k][i]);
for(int j=0; j<m; j++)
if(a[k][i][j]=='S')
sh=k,sx=i,sy=j;
else if(a[k][i][j]=='E')
gh=k,gx=i,gy=j;
}
getchar();
}
int ans=dfs();
if(ans!=-1)
printf("Escaped in %d minute(s).\n",ans);
else
printf("Trapped!\n"); }
return 0;
}

Dungeon Master(逃脱大师)-BFS的更多相关文章

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

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

  2. POJ 2251 Dungeon Master【三维BFS模板】

    Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45743 Accepted: 17256 Desc ...

  3. poj 2251 Dungeon Master 3维bfs(水水)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21230   Accepted: 8261 D ...

  4. Dungeon Master(三维bfs)

    题目链接:http://poj.org/problem?id=2251 题目: Description You are trapped in a 3D dungeon and need to find ...

  5. Dungeon Master (三维bfs)

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

  6. POJ 2252 Dungeon Master 三维水bfs

    题目: http://poj.org/problem?id=2251 #include <stdio.h> #include <string.h> #include <q ...

  7. ZOJ 1940 Dungeon Master【三维BFS】

    <题目链接> 题目大意: 在一个立体迷宫中,问你从起点走到终点的最少步数. 解题分析: 与普通的BFS基本类似,只需要给数组多加一维,并且走的时候多加 上.下这两个方向就行. #inclu ...

  8. Dungeon Master POJ - 2251(bfs)

    对于3维的,可以用结构体来储存,详细见下列代码. 样例可以过,不过能不能ac还不知道,疑似poj炸了, #include<iostream> #include<cstdio> ...

  9. Dungeon Master POJ-2251 三维BFS

    题目链接:http://poj.org/problem?id=2251 题目大意 你被困在了一个三维的迷宫,找出能通往出口的最短时间.如果走不到出口,输出被困. 思路 由于要找最短路径,其实就是BFS ...

  10. POJ 2251 Dungeon Master(三维空间bfs)

    题意:三维空间求最短路,可前后左右上下移动. 分析:开三维数组即可. #include<cstdio> #include<cstring> #include<queue& ...

随机推荐

  1. select 下拉框的复选

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAdUAAAFSCAIAAAArbtLAAAAgAElEQVR4nOydd1gUWaK3Z2/YvXd3v7 ...

  2. C++中的引用和指针

    引用和指针有何区别?何时只能使用指针而不能使用引用?    引用是一个别名,不能为 NULL 值,不能被重新分配:指针是一个存放地址的变量.当需要对变量重新赋以另外的地址或赋值为 NULL 时只能使用 ...

  3. Teradata 认证系列 - 1. TCPP这是个啥

    一看历史,好几年没发帖...正好最近在自学teradata认证(学也不一定学的完,最后也不一定去考,仅仅安慰一下不想碌碌无为的内心) 网上一搜,百度上的中文相关资料简直为0.这个不奇怪,毕竟都没什么人 ...

  4. 学习笔记:MDN的JavaScript

    JavaScript 第一步 什么是JavaScript? 每次当你浏览网页时不只是显示静态信息—— 显示即时更新的内容, 或者交互式的地图,或 2D/3D 图形动画,又或者自动播放视频等,你可以确信 ...

  5. 飞塔Web应用防火墙-FortiWeb

    飞塔Web应用防火墙-FortiWeb 平台: fortiweb 类型: 虚拟机镜像 软件包: linux basic software Fortinet security SSL offloadin ...

  6. 测试MS题

    购物车测试点:  1.界面测试        界面布局.排版是否合理:文字是否显示清晰:不同卖家的商品是否区分明显. 2.功能测试 未登录时: 将商品加入购物车,页面跳转到登录页面,登录成功后购物车数 ...

  7. 平时对Vue的总结

    1.v-bind和v-on的区别 v-bind绑定的函数是立即执行的 v-on是需要一定触发执行的 2.computed和methods的区别 computed的函数是设置属性的 methods的函数 ...

  8. IOS Quarzt2D 手动的释放

    - (void)drawRect:(CGRect)rect { // 1.获取上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2.绘制 ...

  9. IOS NSKeyedArchiver(归档存取数据)

    如果对象是NSString.NSDictionary.NSArray.NSData.NSNumber等类 型,可以直接用NSKeyedArchiver进行归档和恢复 不是所有的对象都可以直接用这种方法 ...

  10. Java生成不重复的随机数

    public class test { public static int[] Randoms(int number) { Random rand = new Random(); //创建一个新随机数 ...