题目:

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是终点位置,‘#’是墙,‘.’是路,问从S出发最少经过多长时间就到达E处; 分析:
和迷宫不同的是,迷宫是平面上东南西北的移动,相当于在大楼里面的一层楼里找出口,而这个题目在迷宫的基础上又增加了上下的移动,即大楼里面的上下层之间的移动,
所以需要建立三维的数组,找到S的位置,移动方向由4个增加到6个,直到找到E为止,如果找遍了所有的能走的地方都没找到出口E,就出不来了!!! AC代码:
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
using namespace std;
char a[][][];
int b[][][];
int L,R,C;
int f[][]={{,-,, ,, },
{, ,,-,, },
{, ,, ,,-}};
int s2[][][];
int flag;
struct Knot
{
int x,y,z;
int step;
};
Knot c,d;
bool search1(int x,int y,int z)
{
return (x>=&&x<=L&&y>=&&y<=R&&z>=&&z<=C);
}
int bfs(int si,int sj,int sk)
{
queue<Knot>s;
c.x=si;
c.y=sj;
c.z=sk;
c.step=;
s.push(c);
while (!s.empty())
{
d=s.front();
s.pop();
c.step=d.step+;
for (int i=;i<;i++)
{
c.x=d.x+f[][i];
c.y=d.y+f[][i];
c.z=d.z+f[][i];
if (search1(c.x,c.y,c.z)&&!b[c.x][c.y][c.z]&&a[c.x][c.y][c.z]!='#')
{
if (a[c.x][c.y][c.z]=='E')
return c.step;
b[c.x][c.y][c.z]=;
s.push(c);
}
}
}
return -;
}
int main()
{
int i,j,k;
int si,sj,sk;
while (cin>>L>>R>>C&&(L!=||R!=||C!=))
{
memset(b,,sizeof(b));
for (i=;i<=L;i++)
for (j=;j<=R;j++)
for (k=;k<=C;k++)
{
cin>>a[i][j][k];
if (a[i][j][k]=='S')
{
si=i;
sj=j;
sk=k;
}
}
flag=bfs(si,sj,sk);
if (flag==-)
cout << "Trapped!" << endl;
else
cout << "Escaped in " << flag << " minute(s)." << endl; }
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. win10下安装cygwin全过程

    简单讲:cygwin就是在windows系统上跑linux和unix的环境,跨平台移植的应用程序移植. 安装步骤: 下载cygwin: 打开官网https://cygwin.com/install.h ...

  2. OutOfMemoryError异常

    1.Java堆溢出 Java堆用于存储对象实例,只要不断地创建对象,并且保证GC Roots到对象之间有可达路径来避免垃圾回收机制清除这些对象,就会在对象数量达到最大堆的容量限制后产生内存溢出异常. ...

  3. ant design for vue 关于table的一些问题

    1.为table添加分页: :pagination="pagination" pagination: { defaultPageSize: 10, showTotal: (tota ...

  4. BBS登录功能

    BBS登录功能 一.后端实现 1.实现验证码 from PIL import Image, ImageDraw, ImageFont import random from io import Byte ...

  5. mysql命令行操作大全

    Mysql安装目录 数据库目录/var/lib/mysql/配置文件/usr/share/mysql(mysql.server命令及配置文件)相关命令/usr/bin(mysqladmin mysql ...

  6. eclipse安装tfs插件

    Eclipse安装TFS插件   1.打开Eclipse.点击菜单栏上的 “Help”——>选择“Install New Software”. 2.在弹出框中输入点击“Add”. 3.在弹出框中 ...

  7. lnmp环境搭建:Centos7 + Nginx1.12.2 + Mysql-5.6.38 + PHP7.2.0

    https://blog.csdn.net/ty_hf/article/details/50622888

  8. redis数据库写入数据时提示redis.exceptions.ResponseError错误

    今天运行Django项目在redis数据库写入数据时提示如下错误: ERROR log 228 Internal Server Error: /image_code/cf9ccd75-d274-45c ...

  9. C++ sizeof 运算符

    sizeof 是一个关键字,它是一个编译时运算符,用于判断变量或数据类型的字节大小. sizeof 运算符可用于获取类.结构.共用体和其他用户自定义数据类型的大小. 使用 sizeof 的语法如下: ...

  10. linear correlation coefficient|Correlation and Causation|lurking variables

    4.4 Linear Correlation 若由SxxSyySxy定义则为: 所以为了计算方便: 所以,可以明白的是,Sxx和Sx是不一样的! 所以,t r is independent of th ...