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. Linux 内核usb_bulk_msg 接口

    usb_bulk_msg 创建一个 USB 块 urb 并且发送它到特定的设备, 接着在返回到调用者之 前等待完成. 它定义为: int usb_bulk_msg(struct usb_device ...

  2. 一个简单的Web服务器-支持Servlet请求

    上接 一个简单的Web服务器-支持静态资源请求,这个服务器可以处理静态资源的请求,那么如何处理Servlet请求的呢? 判断是否是Servlet请求 首先Web服务器需要判断当前请求是否是Servle ...

  3. 【Linux】Terminal中输入一行命令快速移动光标至行首行尾

    Linux: ①快速移动光标至行首 Home或Ctrl+A ②快速移动光标至行尾 End或Ctrl+E ③从光标处开始删除,直到行尾 Ctrl+K ④到下一行 Ctrl+N 或 方向键:↓ ⑤到上一行 ...

  4. Hibernate管理Session

    Hibernate自身提供了三种管理Session对象的方法 Session对象的生命周期与本地线程绑定 Session对象的生命周期与JTA事务绑定 Hibernate委托程序管理Session对象 ...

  5. c++ CArray函数

    CArray属于MFC,是一个数组模板类.MFC的数组类支持的数组类似于常规数组,可以存放任何数据类型.常规数组在使用前必须将其定义成能够容纳所有可能需要的元素,即先确定大小,而MFC数组类创建的对象 ...

  6. Object 与 Function那神奇而混乱的搞基关系

    // Object 与 Function神奇而混乱的搞基关系... Object.__proto__ === Function.prototype; // true Object.__proto__ ...

  7. $loj10156/$洛谷$2016$ 战略游戏 树形$DP$

    洛谷loj Desription Bob 喜欢玩电脑游戏,特别是战略游戏.但是他经常无法找到快速玩过游戏的方法.现在他有个问题. 现在他有座古城堡,古城堡的路形成一棵树.他要在这棵树的节点上放置最少数 ...

  8. openlayers中实现点的拖拽(modify),在layer中增加修改删除point。

    最近忙着整地图,都忘记了总结来沉淀自己,自我检讨一下. 总结一下最近使用openlayer时学习的内容,先说下我的业务逻辑吧,在室内地图中 1,点击新增在地图上新增一个可以拖拽的点,拖拽完成后确定位置 ...

  9. 使用tushare 库查阅交易日历

    资料参考:https://tushare.pro/ 交易日历 接口:trade_cal描述:获取各大交易所交易日历数据,默认提取的是上交所 tushare的版本和更新: 执行命令: pip insta ...

  10. 2020年Java程序员应该学习的10大技术

    对于Java开发人员来说,最近几年的时间中,Java生态诞生了很多东西.每6个月更新一次Java版本,以及发布很多流行的框架,如Spring 5.Spring Security 5和Spring Bo ...