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! 三维的bfs, 总共可以向6个方向走,走一步步数+1, 可以按行读取地图, 把z轴设为最高维度, 基本过程和二维的bfs类似
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <queue>
using namespace std; char maze[][][];
bool v[][][];
int l, r, c; int dir[][] = {{,,},{-,,},{,,},{,-,},{,,},{,,-}}; struct node
{
int x, y, z, t;
}now, Next;
int bx, by, bz;
int ex, ey, ez; int bfs()
{
now.x = bx;
now.y = by;
now.z = bz;
now.t = ;
v[bz][bx][by] = ;
queue<node> q;
q.push(now);
while (!q.empty()) {
now = q.front();
q.pop();
if (now.z == ez && now.x == ex && now.y == ey)
return now.t;
for (int i = ; i < ; i++) {
Next.z = now.z + dir[i][];
Next.x = now.x + dir[i][];
Next.y = now.y + dir[i][];
Next.t = now.t + ;
if (Next.x>=&&Next.x<r&&Next.y>=&&Next.y<c&&Next.z>=&&Next.z<l
&& !v[Next.z][Next.x][Next.y] && maze[Next.z][Next.x][Next.y] != '#')
{
q.push(Next);
v[Next.z][Next.x][Next.y] = ;
}
}
}
return ;
} int main()
{
//freopen("1.txt", "r", stdin);
while (~scanf("%d%d%d", &l, &r, &c)) {
if (l+c+r == ) break;
memset(v, , sizeof(v));
for (int i = ; i < l; i++)
for (int j = ; j < r; j++)
scanf("%s", maze[i][j]); for (int i = ; i < l; i++)
for (int j = ; j < r; j++)
for (int k = ; k < c; k++) {
if (maze[i][j][k] == 'S') {
bz = i; bx = j; by = k;
}
if (maze[i][j][k] == 'E') {
ez = i; ex = j; ey = k;
} }
int ans = bfs();
if (ans == )
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n", ans);
} return ;
}
 

[poj] Dungeon Master bfs的更多相关文章

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

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

  2. POJ2251 Dungeon Master —— BFS

    题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  3. hdu 2251 Dungeon Master bfs

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17555   Accepted: 6835 D ...

  4. poj 2251 Dungeon Master( bfs )

    题目:http://poj.org/problem?id=2251 简单三维 bfs不解释, 1A,     上代码 #include <iostream> #include<cst ...

  5. poj 2251 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 2251 Dungeon Master bfs 难度:0

    http://poj.org/problem?id=2251 bfs,把两维换成三维,但是30*30*30=9e3的空间时间复杂度仍然足以承受 #include <cstdio> #inc ...

  7. POJ 2251 Dungeon Master (BFS最短路)

    三维空间里BFS最短路 #include <iostream> #include <cstdio> #include <cstring> #include < ...

  8. Dungeon Master bfs

    time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u POJ 2251 Descriptio ...

  9. POJ2251 Dungeon Master(bfs)

    题目链接. 题目大意: 三维迷宫,搜索从s到e的最小步骤数. 分析: #include <iostream> #include <cstdio> #include <cs ...

随机推荐

  1. python自动化运维六:paramiko

    paramiko是基于python实现的SSH2远程安全连接,支持认证以及密钥方式,可以实现远程命令执行,文件传输,中间SSH代理等功能.也就是采用SSH的方式进行远程访问.SSH登陆的方式可以参考之 ...

  2. JavaScript及jQuery学习小结

    最近几天学习了很多关于JavaScript和jQuery的文章,稍作梳理后,总结如下. 1.jQuery入门系列 环境搭建 只需引用一个jQuery库文件,即可完成jQuery的环境搭建. 选择器 j ...

  3. 【linux】如何给sudo的root设置环境变量

    如果系统不能通过root登陆,而是需要使用其他用户sudo的方式登陆root,那么root的环境变量很难设置,修改/etc/profile也没有用.可以通过下面这个方式解决 修改sudoer的配置文件 ...

  4. Linux下Mysql数据库忘记root

    系统环境:Red Hat Enterprise Linux Server 6 1.停止mysqld服务 [root@Server huage]# service mysqld stop 2.以跳过授权 ...

  5. 使用Spring AOP实现MySQL数据库读写分离案例分析

    一.前言 分布式环境下数据库的读写分离策略是解决数据库读写性能瓶颈的一个关键解决方案,更是最大限度了提高了应用中读取 (Read)数据的速度和并发量. 在进行数据库读写分离的时候,我们首先要进行数据库 ...

  6. 如何识别真Microsoft服务与非Microsoft服务来定位病毒自己的服务

    在我当网管的那段时间,发现有病毒入侵客户服务器,该病毒伪装自己的进程名,我们在服务中发现其也有伪装成系统服务的服务在运行,占用客户服务器的性能,使得CPU与内存的利用率达到90%以上,并持续时间长,甚 ...

  7. VLAN(虚拟局域网)划分

    VLAN根据不同的需求,可以有多种划分方式: 一:静态划分 基于端口             按VLAN交换机上的物理端口和内部的PVC(永久虚电路)端口来划分 静态划分安全.可靠,易于配置与维护 二 ...

  8. 分享知识-快乐自己:什么是MVC

    1.什么是mvc: Model View Controller,是模型-视图-控制器的缩写,一种软件设计典范,用一种业务逻辑.数据.界面显示分离的方法组织代码,将业务逻辑聚集到一个组件里,在改进和个性 ...

  9. spring2.5和struts1.3.8整合

    第一步:导入对应jar文件 第二步: 1.在web容器中实例化spring容器 <!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpa ...

  10. 理解YOLOv2训练过程中输出参数含义

    原英文地址: https://timebutt.github.io/static/understanding-yolov2-training-output/ 最近有人问起在YOLOv2训练过程中输出在 ...