普及一下知识

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 proxy cache配置和清理

    1.nginx需要编译Purge模块 2.nginx.conf 配置cache: proxy_cache_path  /home/cache/xxx levels=1:2  keys_zone=cac ...

  2. TC2安装方法

    电驴下载TC2英文原版安装文件,3 Disk,安装方法记录如下: cmd.exe chcp 437 挂载安装文件夹1到A盘 subst a: d:\c\Disk1 另开一个cmd,转到A盘,输入ins ...

  3. NUTCH2.3 hadoop2.7.1 hbase1.0.1.1 solr5.2.1部署(三)

     Precondition: hadoop 2.7.1 hbase 0.98.13 solr 5.2.1 / Apache Solr 4.8.1 http://archive.apache.org ...

  4. CAShapeLayer实现圆形进度条效果

    一.CAShapeLayer简单介绍: .CAShapeLayer继承至CALayer,能够使用CALayer的全部属性值 2.CAShapeLayer须要与贝塞尔曲线配合使用才有意义 3.使用CAS ...

  5. Fiddler 抓取 ios 端数据包

    前提条件: 1. Fiddler 工具安装完成,并授权成功,可以完成网页的http 协议拦截. 2. iphone X 一部 ☺ 3. 360wifi 一个[同一局域网内,任何wifi都可以设置,其他 ...

  6. Asp.Net MVC3中如何进行单元测试?

    下面我们就以一个示例演示一下如何进行单元测试? public Model.UserInfo UpdateEntity(Model.UserInfo entity) { db.UserInfo.Atta ...

  7. hdu 4414 Finding crosses【简单模拟】

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4414 CSUST:点击打开链接 Finding crosses Time Limit: 2000/1000 ...

  8. Cloneable 和clone的区别和联系

    设计模式----原型模式时候,涉及到的复制克隆, Cloneable 接口,内部是没有任何方法的, 这个接口其实是一个标记性的接口,和Serializable是一样的,都是标记使用, 在类实现了这个C ...

  9. unity里standard pbr(一)

    关注forwardbase下的 standard.shader #pragma vertex vertBase #pragma fragment fragBase #include "Uni ...

  10. cocos2dx使用cocostudio导出的scene

    local uilocal function createLayerUI() if not ui then ui=cc.Layer:create(); createLayerUI=nil; end r ...