Problem 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,在普通的二维平面图上增加了层数这个维度,除了在同一层东南西北方向上行走之外,还可以在相邻层之间行走,例如:可以从(x,y,0)---->(x,y,1),读懂题意后就很好写了,BFS模板走起
#include <algorithm>
#include <bitset>
//#include <bits/extc++.h>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue> using namespace std;
//using namespace __gnu_pbds #define ll long long
#define maxn 105 char maps[][][];
int dir[][] = {{, , }, {-, , }, {, , }, {, -, }, {, , }, {, , -}}, step[][][];
bool vis[][][];
int L, R, C; struct Node
{
int l, r, c;
} start, end1; bool check(Node x)
{
if (x.l < || x.l >= L || x.r < || x.r >= R || x.c < || x.c >= C || !vis[x.l][x.r][x.c] || maps[x.l][x.r][x.c] == '#')
{
return false;
}
return true;
} void bfs()
{
queue<Node> q;
q.push(start);
step[start.l][start.r][start.c] = ;
vis[start.l][start.r][start.c] = false;
while (!q.empty())
{
Node now = q.front();
q.pop();
for (int i = ; i < ; ++i)
{
Node next = now;
next.l += dir[i][];
next.r += dir[i][];
next.c += dir[i][];
// cout << next.l << " " << next.r << " " << next.c << endl;
// cout << maps[next.l][next.r][next.c] << endl;
if (check(next))
{
//cout << next.l << " " << next.r << " " << next.c << endl;
step[next.l][next.r][next.c] = step[now.l][now.r][now.c] + ;
vis[next.l][next.r][next.c] = false;
q.push(next);
}
}
}
} int main()
{
while (scanf("%d%d%d", &L, &R, &C) && L && R && C)
{
memset(step, -, sizeof(step));
memset(vis, true, sizeof(vis));
for (int i = ; i < L; ++i)
{
for (int j = ; j < R; ++j)
{
for (int k = ; k < C; ++k)
{
scanf(" %c", &maps[i][j][k]);
if (maps[i][j][k] == 'S')
{
start.l = i;
start.r = j;
start.c = k;
}
else if (maps[i][j][k] == 'E')
{
end1.l = i;
end1.r = j;
end1.c = k;
}
}
}
}
bfs();
if (step[end1.l][end1.r][end1.c] != -)
{
printf("Escaped in %d minute(s).\n", step[end1.l][end1.r][end1.c]);
}
else
{
puts("Trapped!");
}
}
return ;
}

Dungeon Master (简单BFS)的更多相关文章

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

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

  2. 棋盘问题(DFS)& Dungeon Master (BFS)

    1棋盘问题 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的 ...

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

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

  4. ZOJ 1940 Dungeon Master 三维BFS

    Dungeon Master Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Desc ...

  5. POJ 2251 Dungeon Master (三维BFS)

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

  6. 【POJ - 2251】Dungeon Master (bfs+优先队列)

    Dungeon Master  Descriptions: You are trapped in a 3D dungeon and need to find the quickest way out! ...

  7. (简单) POJ 2251 Dungeon Master,BFS。

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

  8. poj 2251 Dungeon Master(bfs)

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

  9. 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 ...

随机推荐

  1. [板子]Kruskal

    众所周知求最小生成树的两种方法: 1.Kruskal 2.Prim 这里只挂第一种,因为noip掌握第一种就够了. 两种做法的区别可以参考这个博客:http://blog.csdn.net/molln ...

  2. error:cannot load file (code:5555h);bootauto.ini

    最近发现有的网友在使用Ghost XP盘安装系统的时候,选择一键ghost到C盘出现下面的错误: error:cannot load file (code:5555h);bootauto.ini(或b ...

  3. Vue学习笔记-基本语法

    插值文本(输出文本):{{ }}或v-textHtml(输出Html):v-html 监听属性常用于表单输入时要进行的动作watch : { dataValue:function(val) { }} ...

  4. Stylized Image Caption论文笔记

    Neural Storyteller (Krios et al. 2015) : NST breaks down the task into two steps, which first genera ...

  5. lumen 笔记一

    可以用config()函数和evn()函数来获取 .evn里面的配置内容 config('app.timezone') 获取配置config(['app.timezone' => 'China/ ...

  6. 修改jupyter notebook响应的浏览器

    Windows下更改jupyter notebook默认响应的浏览器为Chrome 1.命令行下输入:jupyter notebook --generate-config 2.C盘中找到并打开文件:C ...

  7. mac 访达修改所有文件夹默认排序方式

    先说个误区,下图只能改变当前目录的排序方式 修改所有目录的排序方式需要在顶部的“显示” 中修改

  8. 金蝶handler中 collection 代码片段理解

    1,AtsOverTimeBillBatchEditHandler中collection的理解 SelectorItemCollection selectors = new SelectorItemC ...

  9. Javascript事件系统

    本文内容 事件基础 事件监听方式 事件默认行为 事件冒泡与事件捕获 事件绑定与事件委托 事件基础 注意:本文不会深入探究Javascript的事件循环. 提到事件,相信每位Javascript开发者都 ...

  10. windows系统下的maven项目放到linux系统中运行时报org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnection这种异常的解决办法

    这个错误的解决办法其实很简单你把连接mysql数据库的那个jar包换成linux版本的就行了: linux版本的连接mysql数据库的jar包链接:http://files.cnblogs.com/f ...