普及一下知识

s.empty() 如果栈为空返回true,否则返回false
s.size() 返回栈中元素的个数
s.pop() 删除栈顶元素但不返回其值
s.top() 返回栈顶的元素,但不删除该元素
s.push() 在栈顶压入新元素

q.empty() 如果队列为空返回true,否则返回false
q.size() 返回队列中元素的个数
q.pop() 删除队列首元素但不返回其值
q.front() 返回队首元素的值,但不删除该元素
q.push() 在队尾压入新元素
q.back() 返回队列尾元素的值,但不删除该元素

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!

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<queue>//队列头文件
#include<stack>//栈头文件
using namespace std;
#define N 35
int level, row, column, visit[N][N][N];//level代表水平方向, row代表行, column代表列,visit为标记数组
//这是他逃跑的六个方向
int dir[6][3]= {{1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}};

char maps[N][N][N];

typedef struct maze//定义结构体,也就是他的坐标和现在用了多少时间
{
int l, r, c, t;
} MAZE;
MAZE n, m, s, e;

bool check(int x, int y, int z)//判断这个点是否符合要求,才可以入队列
{
if(x<0||x>=level||y<0||y>=row||z<0||z>=column||visit[x][y][z]==1||maps[x][y][z]=='#')
return false;
return true;
}

int BFS()
{
queue<MAZE>que;//这是声明存储MAZE类型数据的队列
que.push(s);//入队列

while(que.size())//替换成while(!que.empty())其实也可以
{
m=que.front();//是访问队列的第一个即最低端元素,
que.pop();//出队列,队列遵循先进先出,所以这里取出的是最低端的元素;
if(m.l==e.l&&m.r==e.r&&m.c==e.c)//广搜停止的条件
return m.t;
for(int i=0; i<6; i++)
{
n=m;
n.l+=dir[i][0];
n.r+=dir[i][1];
n.c+=dir[i][2];
n.t++;
if(check(n.l, n.r, n.c))
{
visit[n.l][n.r][n.c]=1;
que.push(n);
}
}
}
return -1;
}

int main()
{
int i, j, k, answer;
while(scanf("%d%d%d", &level, &row, &column), !(level==0&&row==0&&column==0))//这里也可以写成(level!=0||row!=0||column!=0)
{
memset(visit, 0, sizeof(visit));
for(i=0; i<level; i++)
{
for(j=0; j<row; j++)
{
getchar();//这里的getchar是吸收上一行迷宫后面的回车
for(k=0; k<column; k++)
{
scanf("%c", &maps[i][j][k]);
if(maps[i][j][k]=='S')
{
s.l=i;
s.r=j;
s.c=k;
s.t=0;
}
if(maps[i][j][k]=='E')
{
e.l=i;
e.r=j;
e.c=k;
}
}
}
getchar();//这道题level个矩阵后都又跟了一行回车或空格,这个getchar是吸收每个矩阵后面的回车;
}
answer=BFS();
if(answer==-1)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n", answer);
}
return 0;
}

poj 2251 Dungeon Master-搜索进阶-暑假集训的更多相关文章

  1. POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索)

    POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索) Description You ar ...

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

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

  3. BFS POJ 2251 Dungeon Master

    题目传送门 /* BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 */ #include <cstdio> #includ ...

  4. POJ 2251 Dungeon Master(地牢大师)

    p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: ...

  5. POJ.2251 Dungeon Master (三维BFS)

    POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...

  6. poj 2251 Dungeon Master

    http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  7. POJ 2251 Dungeon Master (三维BFS)

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

  8. POJ 2251 Dungeon Master【三维BFS模板】

    Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45743 Accepted: 17256 Desc ...

  9. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

随机推荐

  1. 利用nginx搭建https服务器

    一.HTTPS简介 HTTPS其实是有两部分组成:HTTP + SSL / TLS,也就是在HTTP上又加了一层处理加密信息的模块.服务端和客户端的信息传输都会通过TLS进行加密,所以传输的数据都是加 ...

  2. springBoot+springCloud学习笔记

    尊重原创:https://www.jianshu.com/p/492dfefa2735 SpringBoot 配置优先级 在命令行中传入的参数 如:java -jar storeMs.jar --se ...

  3. Apollo配置中心解惑(一):关于一个portal管理多个环境,要求环境相互之间不影响,独立

    关于作者的回答很官方,不太懂: https://github.com/ctripcorp/apollo/wiki/%E5%88%86%E5%B8%83%E5%BC%8F%E9%83%A8%E7%BD% ...

  4. OpenGL/GLSL数据传递小记(3.x)(转)

    OpenGL/GLSL规范在不断演进着,我们渐渐走进可编程管道的时代的同时,崭新的功能接口也让我们有点缭乱的感觉.本文再次从OpenGL和GLSL之间数据的传递这一点,记录和介绍基于OpenGL3.x ...

  5. Python内置函数之str()

    class str(object="")class str(object=b'', encoding='utf-8', errors='strict') 将其他对象转化为字符串对象 ...

  6. java 原生定时执行程序(ScheduledExecutorService)

    package ThreadPoolTest; import java.util.Date; import java.util.concurrent.*; public class Main { pu ...

  7. android 常用方法总结

    public class Toolkit { /** * * Role:Telecom service providers获取手机服务商信息 <BR> * * 需要加入权限<uses ...

  8. leetCode(37):Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  9. 谨慎使用ArrayList中的subList方法

    转自:https://www.toutiao.com/a6705958780460335619/?tt_from=weixin&utm_campaign=client_share&wx ...

  10. python 利用pymssql连接MSSQL数据库,简单示例

    #-*- coding:GBK -*- import pymssql print 'Connect to the Datebase....' conn = pymssql.connect(host=' ...