一道三维的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. SYN攻击

    一.TCP握手协议 在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接. 第一次握手:建立连接时,客户端发送syn包(syn=j)到服务器,并进入SYN_SEND状态,等待服 ...

  2. wordpress 首页调用文章 不同样式的方法

    <?php $count = 1; $display_categories = array(1); foreach ($display_categories as $category) { ?& ...

  3. PHP初步(上)

    一.php语法 PHP是一种嵌入式脚本语言,所以,我们需要标记出哪些是PHP代码,哪些是html代码.当html和php代码进行混编的时候,文件必须要以php结尾(否则Apache不会将这个文件交给p ...

  4. AtCoder Grand Contest 6

    A - Prefix and Suffix 题意:输入一个整形变量n和两个字符串s,t,使用一些规则求满足条件的最短字符串的长度:规则如下:这个最短字符串的长度不能小于n:它的前n个字符必须与s相同: ...

  5. JSP内置对象---用户登录页面(get和post)

    Login.jsp 页面: <%@ page language="java" import="java.util.*" contentType=" ...

  6. 查询java 类加载的路径

    在Spring 3.x企业应用开发实战中看到一个能经常用到的jsp,记录下,查看类的加载路径, 在jar包冲突引起的奇葩问题时很好用.使用方法 srcAdd.jsp?className=java.ne ...

  7. java基础之 重排序

    重排序通常是编译器或运行时环境为了优化程序性能而采取的对指令进行重新排序执行的一种手段.重排序分为两类:编译期重排序和运行期重排序,分别对应编译时和运行时环境. 在并发程序中,程序员会特别关注不同进程 ...

  8. 使用JavaScript实现复选框全选与取消的功能

    实现效果: html代码: <body> <input type="checkbox" id="checkAll"/>全选<br& ...

  9. centos7安装jdk8

    1.查看是否已经安装了jdk rpm -qa|grep java 表示没有安装jre 如果已经安装请先卸载 2.查看可安装的java环境 yum list java* 3.安装 java-1.8.0- ...

  10. TCPCopy使用

      http://www.thinkingbar.com/2014/04/17/tcpcopy使用/ 主题 技术 一.应用背景 主要用于系统的稳定性测试.它可以复制线上服务器的请求,通过修改TCP/I ...