一道三维的BFS

Dungeon Master

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 24003 Accepted: 9332

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!

题意:

一个能向东西南北前后走的人,,”#“是墙,“.”是路,问从“S”到“E” 的最少步数。

一个明显的BFS ,,, 除了是三维的没有任何难度

#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
using namespace std;
char a[66][66][66];
int l,r,c,sx,sy,sz,vis[66][66][66];
int xx[]={1,-1,0,0,0,0},yy[]={0,0,1,-1,0,0},zz[]={0,0,0,0,1,-1};
int bfs()
{
queue<int> q,w,e;
q.push(sx);w.push(sy);e.push(sz);
while(!q.empty())
{
int x=q.front(),y=w.front(),z=e.front();
q.pop();w.pop();e.pop();
if(a[x][y][z]=='E') return vis[x][y][z];
for(int i=0;i<6;i++)
{
int dx=x+xx[i],dy=y+yy[i],dz=z+zz[i];
if(dx<1||dx>l||dy<1||dy>r||dz<1||dz>c||a[dx][dy][dz]=='#'||vis[dx][dy][dz])
continue;
q.push(dx);w.push(dy);e.push(dz);
vis[dx][dy][dz]=vis[x][y][z]+1;
}
}
return 0;
}
int main()
{
while(scanf("%d%d%d",&l,&r,&c)&&l)
{
memset(a,0,sizeof(a));
memset(vis,0,sizeof(vis));
for(int i=1;i<=l;i++)
{
for(int j=1;j<=r;j++)
{
for(int k=1;k<=c;k++)
{
cin>>a[i][j][k];
if(a[i][j][k]=='S')
{
sx=i;sy=j;sz=k;
}
}
}
}
int k=bfs();
k?printf("Escaped in %d minute(s).\n",k):printf("Trapped!\n");
}
}

POJ 2251 BFS(简单)的更多相关文章

  1. Dungeon Master POJ - 2251(bfs)

    对于3维的,可以用结构体来储存,详细见下列代码. 样例可以过,不过能不能ac还不知道,疑似poj炸了, #include<iostream> #include<cstdio> ...

  2. POJ - 2251 bfs [kuangbin带你飞]专题一

    立体bfs,共有六个方向: const int dx[] = {0,0,1,-1,0,0}; const int dy[] = {1,-1,0,0,0,0}; const int dz[] = {0, ...

  3. POJ 2251 bfs

    DESCRIPTION:给你一个三维的迷宫.问你是否能从起点走到终点.如果能,输出最小步数.对我来说难得就是我没有想到怎么把他给你的三维图转换成map.恩..好像解题报告上说.只要是这种的最短路都要用 ...

  4. 【BFS】POJ 2251

    POJ 2251 Dungeon Master 题意:有一个地图,三维,走的方向是上下,左右,前后.问你最小步数从起始点走到出口. 思路:三维的BFS,就是多加一组状态,需要细心(不细心如我就找了半个 ...

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

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

  6. 【POJ 2251】Dungeon Master(bfs)

    BUPT2017 wintertraining(16) #5 B POJ - 2251 题意 3维的地图,求从S到E的最短路径长度 题解 bfs 代码 #include <cstdio> ...

  7. POJ 2251 Dungeon Master bfs 难度:0

    http://poj.org/problem?id=2251 bfs,把两维换成三维,但是30*30*30=9e3的空间时间复杂度仍然足以承受 #include <cstdio> #inc ...

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

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

  9. BFS POJ 2251 Dungeon Master

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

随机推荐

  1. mac 端口被占用及kill端口

    在本地部署 Web 应用时我有遇到过某网络端口已经被其他程序占用的情况,这时候就需要先退出占用该端口的进程,我们可以通过“终端”来实现结束占用某特定端口的进程 1.打开终端,使用如下命令: lsof ...

  2. ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务解决

    先看oracle的监听和oracle的服务是否都启动了. 启动oracle监听:cmd命令行窗口下,输入lsnrctl start,回车即启动监听. 查看oracle的sid叫什么,比如创建数据库的时 ...

  3. hibernate开发(2)

    1 hibernate 的缓存机制 在程序运行中,hibernate要不断访问物理数据库,为了降低访问频率,提升性能,会复制一部分数据到缓存中,使得hibernate可以从缓存中读写数据,然后在特定时 ...

  4. [转载]va_start和va_end使用详解

    va_start和va_end使用详解 原文地址:http://www.cnblogs.com/hanyonglu/archive/2011/05/07/2039916.html 本文主要介绍va_s ...

  5. AppCode 2016.2.3 发布,支持 Swift3 的特性

    AppCode 2016.2.3 (build 162.2380.5)发布了,AppCode 是一个全新的 Objective-C.Swift 的集成开发环境,用于帮助开发 Mac.iPhone 和 ...

  6. 《1---关于解决MySQL在控制台插入中文乱码问题》

    说明:以下所有操作都是基于我个人的电脑及示例,读者可以参考我这个解决过程,去解决自己的问题,如有其它疑问,欢迎留言交流. 首先来看看我遇到的问题: [1]查看数据库: [2]使用test数据库: [3 ...

  7. 搭建Android开发环境简要步骤

    (一)安装JDK JDK下载地址 http://www.oracle.com/technetwork/java/javase/downloads/index.html 在Linux终端输入如下命令,设 ...

  8. 【其他】win7创建wifi热点共享给手机使用

    出门在外,有时候网络有诸多不便,需要用笔记本创建wifi热点给手机用:本人测试xp怎么配置都不好使,但win7有可行的方案,不依赖第三方软件. 详述如下: 场景一:win7 + A(PC机)(用无线连 ...

  9. Uber优步宁波司机注册正式开始啦! UBER宁波司机注册指南!

      自2012年Uber开始向全球进军以来,目前已进入全球56个国家和地区的市场,在全球超过270个城市提供服务, 而Uber公司的估值已高达412亿美元. [目前开通Uber优步叫车服务的中国城市] ...

  10. ASP.NET常用函数(参考用)

    Abs(number) 取得数值的绝对值. Asc(String) 取得字符串表达式的第一个字符ASCII 码. Atn(number) 取得一个角度的反正切值. CallByName (object ...