普及一下知识

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. JavaSE入门学习21:Java面向对象之接口(interface)(二)

    一接口实现的多态 在上一篇博文:JavaSE入门学习20:Java面向对象之接口(interface)(一)中提到了接口的实现存在多态性,那么 这一篇主要就要分析接口实现的多态. 实例一 Test.j ...

  2. org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

    © 版权声明:本文为博主原创文章,转载请注明出处 1.问题描述 启动hibernate测试案例时报错如下: 2.解决方案: 2.1 第一次解决:MySQL驱动版本太高.使用的hibernate版本为5 ...

  3. Android IntentService全然解析 当Service遇到Handler

    转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/47143563: 本文出自:[张鸿洋的博客] 一 概述 大家都清楚.在Andro ...

  4. oracle中直方图的使用

    本文从不绑定变量和绑定变量两种情况讨论直方图的作用 一.不绑定变量 SQL> create table test(name varchar2(10));表已创建.SQL> insert i ...

  5. Rserve方式连接别的服务器

    Rserve Rserve的方式,这是一个基于TCP/IP的服务器,通过二进制协议传输数据,可以提供远程连接,使得客户端语言能够调用R 既然是TCP/IP 就可以在不同的机器上运行了 事实上官网给出了 ...

  6. js中的DOM节点

    文档对象模型DOM(Document Object Model)定义访问和处理HTML文档的标准方法. DOM 将HTML文档呈现为带有元素.属性和文本的树结构(节点树). 把上面的代码拆分为Dom节 ...

  7. ubuntu防火墙 ufw配置

    https://www.cnblogs.com/ylan2009/articles/2321136.html

  8. 【JavaEE】Springmvc搭建方法及example

    现在介绍SSH的文章很多,但是适合自己需求的却经常找不到,这些东西呢,会了之后总会感觉别人的程序哪里哪里别扭,会之前呢就感觉很混乱,而且SSH的官方文档,至少在我看来是“会者勉强能看.不会者一片迷茫” ...

  9. C语言合并两个集合(L,L1) 将L1中不在L中的元素插入到L线性表中

    void main(){ Sqlist L,L1; InitList(&L); InitList(&L1); ListInsert(&L, 1, 2); ListInsert( ...

  10. 九度OJ 1261:寻找峰值点 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:500 解决:37 题目描述: 给定一个整数序列,该整数序列存在着这几种可能:先递增后递减.先递减后递增.全递减.全递增. 请找出那个最大值的 ...