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!

就是一个人被逮到地牢里面了,地牢有好几层(L),每层R*C,标#的地方不能走,每次可以上、下、左、右、前、后走一个相邻的格子,花费1分钟。问你从S走,能不能逃出这个地牢(走到E)。
这是我认真的写的第一个dfs,是不是有点晚呢?呵呵。总结一下dfs的几个要点吧。 1.struct node 用于标记点的坐标,和到达(如果能)该店的步数。
2.vis dir 数组,分别表示 是否访问过 每次走的方向
3.check函数 :检查3点 1->是否超出边界 2->是否走过 3->是否能走
4.基于队列实现的bfs函数,这里详细解释
首先我们建立一个node的队列q,我们先把起点加进队列。从当q的队首开始,用temp取得这个队首,q.pop().将通过temp能走到的node再加入到队列里面,同时判断是否到达终点。
这样知道q这个队列空了,我们就走完了所有可能的路了。 代码如下:
 #include <cstdio>
#include <iostream>
#include <cmath>
#include <queue>
#include <cstring>
using namespace std;
struct node
{
int l,x,y;
int step;
};
int L,R,C;
int el,ex,ey,sl,sx,sy,ans;
char mp[][][];
bool vis[][][];
int dir[][]={,,, -,,, ,,, ,-,, ,,, ,,-};
bool check(node now)
{
if (now.l>=L||now.l<||
now.x>=R||now.x<||
now.y>C||now.y<)
return ;
if (mp[now.l][now.x][now.y]=='#'||vis[now.l][now.x][now.y])
return ;
return ;
}
void bfs (int level,int xx,int yy)
{
queue<node> q;
node now;
now.l=level,now.x=xx,now.y=yy,now.step=;
vis[level][xx][yy]=true;
q.push(now);
while (!q.empty())
{
node temp=q.front();
q.pop();
for (int i=;i<;++i)
{
now.l=temp.l+dir[i][];
now.x=temp.x+dir[i][];
now.y=temp.y+dir[i][];
now.step=temp.step+;
if (check(now))
{
if (mp[now.l][now.x][now.y]=='E')
{
ans=now.step;
return;
}
vis[now.l][now.x][now.y]=true;
q.push(now);
}
}
}
}
int main()
{
//freopen("de.txt","r",stdin);
while (~scanf("%d%d%d",&L,&R,&C))
{
if (L==&&R==&&C==)
break;
el=ex=ey=;
sl=sx=sy=;
memset(vis,false,sizeof vis);
for (int i=;i<L;++i)
{
for (int j=;j<R;++j)
{
for (int k=;k<C;++k)
{
cin>>mp[i][j][k];
if (mp[i][j][k]=='S')
{
sl=i,sx=j,sy=k;
}
if (mp[i][j][k]=='E')
{
el=i,ex=j,ey=k;
}
}
}
}
ans=-;
bfs(sl,sx,sy);
if (ans!=-)
printf("Escaped in %d minute(s).\n",ans);
else
printf("Trapped!\n");
}
return ;
}
 

POJ 2251 Dungeon Master(dfs)的更多相关文章

  1. poj 2251 Dungeon Master(bfs)

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

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

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

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

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

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

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

  5. POJ 2251 Dungeon Master (非三维bfs)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 55224   Accepted: 20493 ...

  6. POJ 2251 Dungeon Master(多层地图找最短路 经典bfs,6个方向)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 48380   Accepted: 18252 ...

  7. POJ 2251 Dungeon Master(广搜,三维,简单)

    题目 简单的3d广搜,做法类似与 hdu 的 胜利大逃亡 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<str ...

  8. POJ 2251 Dungeon Master(三维空间bfs)

    题意:三维空间求最短路,可前后左右上下移动. 分析:开三维数组即可. #include<cstdio> #include<cstring> #include<queue& ...

  9. POJ - 2251 Dungeon Master(搜索)

    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...

随机推荐

  1. hdu 5120 Intersection (圆环面积相交->圆面积相交)

    Problem Description Matt is a big fan of logo design. Recently he falls in love with logo made up by ...

  2. Word中页码及目录、参考文献的制做方法

    1.页码从正文开始 1.要想从哪里显示第一页,就在这页的前一页的最后一行最后的地方,插入分隔符---下一页2.然后在你想显示第一页的那一页双击页脚处,点击取消掉“链接到前一条页眉”.(这是为了取消原来 ...

  3. [CSP-S模拟测试]:施工(DP+单调栈+前缀和)

    题目描述 小$Y$家门前有一条街道,街道上顺序排列着$n$幢建筑,其中左起第$i$幢建筑的高度为$h_i$.小$Y$定义街道的不美观度为所有相邻建筑高度差的绝对值之和乘上常数$c$,为了改善街道环境, ...

  4. [20190727NOIP模拟测试9]单(single) 题解(树上dp)

    啊啊啊啊啊啊啊啊考场上差一点就A掉了5555 千里之堤溃于蚁穴……鬼知道最后一步那么显然的柿子我为什么没考虑用上…… 观察数据范围可知,出题人期望我们想出一个$O(n)$的做法 当然也有可能是$O(n ...

  5. Unity Shader之模板测试

    Unity Shader之模板测试 一沙一世界,一花一天堂 一.Stencil testing 渲染管线     当片段着色器处理完一个片段之后,模板测试(Stencil Test)会开始执行,和深度 ...

  6. centos7运行yum报如下提示:Run "yum repolist all" to see the repos you have

    centos7运行yum报如下提示: There are no enabled repos. Run "yum repolist all" to see the repos you ...

  7. 通过注册表修改IE的Internet选项

    Internet Explorer 安全区域设置存储在以下注册表子项下面: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\I ...

  8. kubernetes原理

    kubernetes 核心组件 etcd:保存整个集群的状态 apiserver:提供资源操作的唯一入口,并提供认证.授权.访问控制.api注册和发现等机制 controller manager:负责 ...

  9. ORM模型类介绍,

    所有的软件开发过程中,都会涉及到对象和关系型数据库,在用户层面和业务逻辑层面,程序员编写代码都是面向对象的,当我们对象的信息发生变化的时候,都需要将对应的信息,传到关系型数据库中.而在此之前,需要我们 ...

  10. webservice 学习笔记 1

    Webservice----------->跨语言服务调用 (视频学习总结) 1-1.有OA系统 需要添加一个功能,登录之后显示天气情况 此时可以使用Webservice eg1: 气象局自己有 ...