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. 【35.12%】【POJ 1988】Cube Stacking

    Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 24007 Accepted: 8432 Case Time Limit: 100 ...

  2. 一培训机构设计的学习android课程内容:供大家参考

    转自:http://www.cnblogs.com/csj007523/archive/2011/06/16/2082682.html 一培训机构设计的学习android课程内容:供大家参考 第一阶段 ...

  3. JMeter数据库测试计划

    在系统上安装数据库服务器之后. 按着这些次序: 创建名为testdb的数据库. 创建表 - tb_user. 将记录插入到tb_user表中. 下图显示了创建的数据库及其记录. 注意:您需要将相应的J ...

  4. There is no Action mapped for namespace [/] and action name [login] associate解决办法 .

    写了一个JSP项目,在配置struts2时遇到了这个错误,在网上逛了一大圈后终于解决了这个问题.具体解决方法是: 1.struts.xml的名字和位置 这里特别提一点,很多人遇到这个错误都是名字错误, ...

  5. c++ 队列算法

    include using namespace std; #define Maxsize 5 typedef int DataType; typedef struct Queue { DataType ...

  6. 分布式大牛详解Zookeeper底层原理

    很多学员都在反馈,说zk很难学,学的不是很明白,在这里,我继续带着大家详解一遍Zookeeper 首先zk是什么呢首先肯定是一个个分布式服务框架,是Apache Hadoop 的一个子项目,它主要是用 ...

  7. no supported authentication methods avaiable

    在git(小乌龟)向github远程推送(push)文件是会报一个异常 no supported authentication methods avaiable 解决方法:因为git(小乌龟)和Git ...

  8. 「2015南阳CCPC D」金砖 解题报告

    金砖 Problem 有一个长度为L的板凳,可以放一排金砖,金砖不能重叠.特别的,摆放的金砖可以超出板凳,前提是必须保证该金砖不会掉下去,即该金砖的重心必须在板凳上. 每块金砖都一个长度和价值,且金砖 ...

  9. 1041 考试座位号 (15 分)C语言

    每个 PAT 考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位.正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考 ...

  10. 通俗易懂理清mybatis中SqlSessionSql、SqlSessionTemplate、SessionFactory和SqlSessionFactoryBean之间的关系

    我潇洒的灰大狼又回来啦.今天送大家的一句话是: 保持耐心,永远年轻,永远热泪盈眶. 前言 先容我哭一会儿,呜呜呜~昨晚写了一半的文章,还没保存就盖上盖子准备回家,拔下电源准备把电脑塞进书包带回家完成时 ...