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!

Source

思路:比较二维的增加两种转移方式,其他基本不变。
代码:
 #include "cstdio"
#include "stdlib.h"
#include "iostream"
#include "algorithm"
#include "string"
#include "cstring"
#include "queue"
#include "cmath"
#include "vector"
#include "map"
#include "set"
#define mj
#define db double
#define ll long long
using namespace std;
const int N=1e8+;
const int mod=1e9+;
const ll inf=1e16+;
bool v[][][];
int dx[]={,,,,-,},dy[]={,,,,,-},dz[]={-,,,,,};
int a,b,c;
char s[][][];
int d[][][];
typedef struct P{
int x,y,z;
P(int c,int d,int e){
z=c,x=d,y=e;
}
};
void bfs(P t){
queue<P> q;
for(int i=;i<;i++)
for(int j=;j<;j++)
for(int k=;k<;k++)
d[i][j][k]=N;
q.push(t);
memset(v,, sizeof(v));
d[t.z][t.x][t.y]=;
v[t.z][t.x][t.y]=;
while(q.size()){
P p=q.front();
q.pop();
if(s[p.z][p.x][p.y]=='E'){
printf("Escaped in %d minute(s).\n",d[p.z][p.x][p.y]);
return;
} for(int i=;i<;i++){
int nx=p.x+dx[i],ny=p.y+dy[i],nz=p.z+dz[i];
if(<=nx&&nx<b&&<=ny&&ny<c&&<=nz&&nz<a&&s[nz][nx][ny]!='#'&&!v[nz][nx][ny]){
d[nz][nx][ny]=d[p.z][p.x][p.y]+;
q.push(P(nz,nx,ny));
v[nz][nx][ny]=; }
} }
printf("Trapped!\n"); }
int main()
{
while(scanf("%d%d%d",&a,&b,&c)==,a||b||c){
int t=a;
int x,y,z;
memset(s,'\0', sizeof(s));// 一开始把x,y,z搞反了,调了好一会
for(int i=;i<t;i++){
for(int j=;j<b;j++){
scanf("%s",s[i][j]);
for(int k=;k<c;k++)
if(s[i][j][k]=='S') x=j,y=k,z=i;
}
}
bfs(P(z,x,y));
}
return ;
}

POJ 2251 三维BFS(基础题)的更多相关文章

  1. POJ:Dungeon Master(三维bfs模板题)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16748   Accepted: 6522 D ...

  2. poj 2251 三维地图最短路径问题 bfs算法

    题意:给你一个三维地图,然后让你走出去,找到最短路径. 思路:bfs 每个坐标的表示为 x,y,z并且每个点都需要加上时间 t struct node{ int x, y, z; int t;}; b ...

  3. POJ 3320 尺取法(基础题)

    Jessica's Reading Problem Description Jessica's a very lovely girl wooed by lots of boys. Recently s ...

  4. ZOJ - 3890 Wumpus(BFS基础题)

    Wumpus Time Limit: 2 Seconds      Memory Limit: 65536 KB One day Leon finds a very classic game call ...

  5. Poj(2225),三维BFS

    题目链接:http://poj.org/problem?id=2225 这里要注意的是,输入的是坐标x,y,z,那么这个点就是在y行,x列,z层上. 我竟然WA在了结束搜索上了,写成了输出s.step ...

  6. POJ 2777 线段树基础题

    题意: 给你一个长度为N的线段数,一开始每个树的颜色都是1,然后有2个操作. 第一个操作,将区间[a , b ]的颜色换成c. 第二个操作,输出区间[a , b ]不同颜色的总数. 直接线段树搞之.不 ...

  7. Dungeon Master POJ - 2251 [kuangbin带你飞]专题一 简单搜索

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

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

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

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

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

随机推荐

  1. Libevent源码分析—event, event_base

    event和event_base是libevent的两个核心结构体,分别是反应堆模式中的Event和Reactor.源码分别位于event.h和event-internal.h中 1.event: s ...

  2. centos rabbitmq 安装

    MQ 的一个产品[消息队列] rabbitmq 的本质<1>rabbitmq 是用什么语言编写的? => erlang<2>rabbitmq 其实是遵循amqp 协议的一 ...

  3. Java常用类之【八种基本数据类型】

    一.装箱和拆箱 装箱:将基本数据类型包装为对应的包装类对象 拆箱:将包装类对象转换成对应的基本数据类型 JDK5.0中为基本数据类型提供了自动装箱(boxing).拆箱(unboxing)功能 二.八 ...

  4. Android搞事篇——使用Intent跳转界面

    跳转页面基本分为三个步骤: 1.初始化一个intent:(一个intent就够用了): 2.传入intent参数: 3.调用startactivity();实现跳转页面 具体操作如下 首先你需要一个项 ...

  5. mysql revise

    DATABASE create database db_name; use db_name; alter database db_name; drop database db_name; show d ...

  6. 2015阿里巴巴安全峰会PPT

    有幸参加了阿里巴巴安全峰会,不得不佩服阿里巴巴神盾局,真牛B!然后亲眼目睹了第二天的各大厂商牛(zhuang)B人才上台演讲,有被捧的,有被喷的,呵呵.总的来说,大家的分享精神还是阔以的. 下面是会议 ...

  7. 高防TTCDN

    TCDN是深圳市云中漫网络科技公司高防CDN产品的品牌名称,既可以防御,也可以达到加速的效果,价格实惠.TTCDN适用于WEB应用,可以隐藏源站服务器IP,有效的减轻源站服务器压力,加快全国各地区线路 ...

  8. 编写一个简单的Web Server

    编写一个简单的Web Server其实是轻而易举的.如果我们只是想托管一些HTML页面,我们可以这么实现: 在VS2013中创建一个C# 控制台程序 编写一个字符串扩展方法类,主要用于在URL中截取文 ...

  9. 基于android的语音识别

    1.注册账户,添加应用 2.针对android平台的选择应用,下载SDK 3.将SDK的libs下文件拷贝到工程的libs目录下 4.添加用户权限 <uses-permission androi ...

  10. 解决Ubuntu SMPlayer播放视频无声音问题

    问题:Ubuntu Kylin 14.04 系统默认装好之后,smplayer播放视频都是正常的,但最近可能由于一些误设置,导致smplayer播放任何格式的视频都无声.解决方法:由于ALSA是Lin ...