题目链接:http://poj.org/problem?id=2251

题目:

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!

题意:你处在一个三维的地牢里,从S出发逃到出口E,问最少要跑多远。 思路:这题虽然是一个三维的地图,但是做法和二维的没多大区别,不过要从当前层到其他层的要求是你所在位置为非#,且你将到的那层的这个位置也是非#。 代码实现如下:
 #include <queue>
#include <cstdio>
#include <cstring>
using namespace std; const int inf = 0x3f3f3f3f;
int l, r, c, ans;
int sx, sy, sz;
char mp[][][];
int vis[][][]; struct node {
int x, y, z, step;
}nw, nxt; int dx[] = {, -, , , , }, dy[] = {, , , -, , },
dz[] = {, , , , , -}; void bfs(int z, int x, int y) {
vis[z][x][y] = ;
nw.z = z, nw.x = x, nw.y = y, nw.step = ;
queue<node> q;
q.push(nw);
while(!q.empty()) {
nw = q.front(), q.pop();
if(mp[nw.z][nw.x][nw.y] == 'E') {
ans = nw.step;
return;
}
for(int i = ; i < ; i++) {
nxt.z = nw.z + dz[i];
nxt.x = nw.x + dx[i];
nxt.y = nw.y + dy[i];
if(nxt.z >= && nxt.z < l && nxt.x >= && nxt.x < r && nxt.y >= && nxt.y < c && vis[nxt.z][nxt.x][nxt.y] == && mp[nxt.z][nxt.x][nxt.y] != '#') {
nxt.step = nw.step + ;
vis[nxt.z][nxt.x][nxt.y] = ;
q.push(nxt);
}
}
}
} int main() {
while(~scanf("%d%d%d", &l, &r, &c) && (l + r + c)) {
for(int i = ; i < l; i++) {
for(int j = ; j < r; j++) {
scanf("%s", mp[i][j]);
for(int k = ; k < c; k++) {
if(mp[i][j][k] == 'S') {
sx = j, sy = k, sz =i;
}
}
}
}
memset(vis, , sizeof(vis));
ans = inf;
bfs(sz, sx, sy);
if(ans >= inf) {
printf("Trapped!\n");
} else {
printf("Escaped in %d minute(s).\n", ans);
}
}
return ;
}
												

Dungeon Master(三维bfs)的更多相关文章

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

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

  2. POJ 2251 Dungeon Master (三维BFS)

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

  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. 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. UVa532 Dungeon Master 三维迷宫

        学习点: scanf可以自动过滤空行 搜索时要先判断是否越界(L R C),再判断其他条件是否满足 bfs搜索时可以在入口处(push时)判断是否达到目标,也可以在出口处(pop时)   #i ...

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

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

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

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

  9. Dungeon Master (简单BFS)

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

  10. POJ 2252 Dungeon Master 三维水bfs

    题目: http://poj.org/problem?id=2251 #include <stdio.h> #include <string.h> #include <q ...

随机推荐

  1. 【beta】Scrum站立会议第1次....11.3

    beta阶段,我们nice!团队将进行为期两周的冲刺,Scrum站立会议10次. 小组名称:nice! 组长:李权 成员:于淼  刘芳芳韩媛媛 宫丽君 项目内容:约跑app(约吧) 时间:2016.1 ...

  2. Linux和Windows文件路径

    linux系统下的文件夹路径和window下的不一样,windows下就需要写成“\\photos"因为java会把第一个"\"当成转义字符给“吃了”.但在linux下就 ...

  3. react-event-pooling

    react-event-pooling 事件池 https://codesandbox.io/s/3xp4y9zp7q https://reactjs.org/docs/events.html#eve ...

  4. WPF通过NPIO读写Excel操作

    自已摸索实现了对excel简单的上传和下载并做了一个封装类,下面分享一下. 先去官网:http://npoi.codeplex.com/下载需要引入dll(可以选择.net2.0或者.net4.0的d ...

  5. bzoj4501 旅行

    题面: 小C来到了F国,小C想好好地参观F国.F国可以看一个有n个点m条边的有向无环图,小C刚开始站在1号点.假设现在小C站在x号点: 1.点x没有出边,结束旅游. 2.点x有o条出边,小C等概率地选 ...

  6. [SOJ #47]集合并卷积

    题目大意:给你两个多项式$A,B$,求多项式$C$使得:$$C_n=\sum\limits_{x|y=n}A_xB_y$$题解:$FWT$,他可以解决形如$C_n=\sum\limits_{x\opl ...

  7. BZOJ1801:[AHOI2009]中国象棋——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1801 https://www.luogu.org/problemnew/show/P2051 这次小 ...

  8. BZOJ3675 [Apio2014]序列分割 【斜率优化dp】

    3675: [Apio2014]序列分割 Time Limit: 40 Sec  Memory Limit: 128 MB Submit: 3366  Solved: 1355 [Submit][St ...

  9. BZOJ [Ctsc2002] Award 颁奖典礼 解题报告

    [Ctsc2002] Award 颁奖典礼 Description IOI2002的颁奖典礼将在YONG-IN Hall隆重举行.人们在经历了充满梦幻的世界杯之后变得更加富于情趣.为了使颁奖典礼更具魅 ...

  10. 基于DCT系数的实时监控中运动目标检测

    本文的主要内容来自2009 Advanced Video and Signal Based Surveillance会议的一篇论文“Real-Time Moving Object Detection ...