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. Oracle 包的学习

    (1)包是一种数据库对象,相当于一个容器.将逻辑上相关的过程.函数.变量.常量和游标组合成一个更大的单位.用户可以从其他 PL/SQL 块中对其进行引用 (2)包类似于C++和JAVA语言中的类,其中 ...

  2. SQL_2008安装教程(完整版)

    Win 7 win xp系统中SQL2008安装注意事项一:SQL2008 镜像下载地址 http://download.microsoft.com/download/4/C/4/4C402E48-0 ...

  3. python中的open()函数

    定义: python open() 函数用于打开一个文件,创建一个 file 对象,相关的方法才可以调用它进行读写 参数: 模式 描述 r 以只读方式打开文件.文件的指针将会放在文件的开头.这是默认模 ...

  4. C++ win32 dll 引用外部CLR,加载托管程序集异常-Error 10 error LNK2019: unresolved external symbol _CLRCreateInstancet

    异常: Error 10 error LNK2019: unresolved external symbol _CLRCreateInstance@12 referenced in function ...

  5. 显示等待WebDriverWait+EC

    参考:https://www.cnblogs.com/yoyoketang/p/6538505.html 百度搜索关键字,等待搜索结果页面显示完成后,验证搜索结果的第一条记录 通过WebDriverW ...

  6. 美化Windows

    更改壁纸 https://www.omgubuntu.co.uk/2010/09/a-look-back-at-every-ubuntu-default-wallpaper google: ubunt ...

  7. 初识redis基础

    一.redis 的五大数据类型: 1.String(字符串): 2.List(列表): 3.Set(集合): 4.Hash(哈希,类似于Java里的Map); 5.Zset(sorted set:有序 ...

  8. input只读效果

    有两种方式可以实现input的只读效果:disabled 和 readonly. 自然两种出来的效果都是只能读取不能编辑,可是两者有很大不同. Disabled说明该input无效,及其value不会 ...

  9. Docker 清理容器 log 日志

    原文 Docker 清理容器 log 日志 docker logs <容器ID> 是常用命令,来查看容器运行日志,但时间长了之后,就会发现越来越慢,log 太多了,这时就需要清理一下. 先 ...

  10. OAuth授权登录

    一.写在前面 日常生活中,我们经常看到到一个网站时,需要登录的时候,都提供了第三方的登录,也就是说你可以使用你的微信,QQ,微博等账号进行授权登录.那么这个认证登录的东西到底是什么呢? 微信授权登录页 ...