POJ 2251 三维BFS(基础题)

时间:2022-09-16 13:51:49
Dungeon Master

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!

Source

思路:比较二维的增加两种转移方式,其他基本不变。
代码:
 #include "cstdio"
#include "stdlib.h"
#include "iostream"
#include "algorithm"
#include "string"
#include "cstring"
#include "queue"
#include "cmath"
#include "vector"
#include "map"
#include "set"
#define mj
#define db double
#define ll long long
using namespace std;
const int N=1e8+;
const int mod=1e9+;
const ll inf=1e16+;
bool v[][][];
int dx[]={,,,,-,},dy[]={,,,,,-},dz[]={-,,,,,};
int a,b,c;
char s[][][];
int d[][][];
typedef struct P{
int x,y,z;
P(int c,int d,int e){
z=c,x=d,y=e;
}
};
void bfs(P t){
queue<P> q;
for(int i=;i<;i++)
for(int j=;j<;j++)
for(int k=;k<;k++)
d[i][j][k]=N;
q.push(t);
memset(v,, sizeof(v));
d[t.z][t.x][t.y]=;
v[t.z][t.x][t.y]=;
while(q.size()){
P p=q.front();
q.pop();
if(s[p.z][p.x][p.y]=='E'){
printf("Escaped in %d minute(s).\n",d[p.z][p.x][p.y]);
return;
} for(int i=;i<;i++){
int nx=p.x+dx[i],ny=p.y+dy[i],nz=p.z+dz[i];
if(<=nx&&nx<b&&<=ny&&ny<c&&<=nz&&nz<a&&s[nz][nx][ny]!='#'&&!v[nz][nx][ny]){
d[nz][nx][ny]=d[p.z][p.x][p.y]+;
q.push(P(nz,nx,ny));
v[nz][nx][ny]=; }
} }
printf("Trapped!\n"); }
int main()
{
while(scanf("%d%d%d",&a,&b,&c)==,a||b||c){
int t=a;
int x,y,z;
memset(s,'\0', sizeof(s));// 一开始把x,y,z搞反了,调了好一会
for(int i=;i<t;i++){
for(int j=;j<b;j++){
scanf("%s",s[i][j]);
for(int k=;k<c;k++)
if(s[i][j][k]=='S') x=j,y=k,z=i;
}
}
bfs(P(z,x,y));
}
return ;
}

POJ 2251 三维BFS(基础题)的更多相关文章

  1. POJ&colon;Dungeon Master&lpar;三维bfs模板题)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16748   Accepted: 6522 D ...

  2. poj 2251 三维地图最短路径问题 bfs算法

    题意:给你一个三维地图,然后让你走出去,找到最短路径. 思路:bfs 每个坐标的表示为 x,y,z并且每个点都需要加上时间 t struct node{ int x, y, z; int t;}; b ...

  3. POJ 3320 尺取法&lpar;基础题)

    Jessica's Reading Problem Description Jessica's a very lovely girl wooed by lots of boys. Recently s ...

  4. ZOJ - 3890 Wumpus(BFS基础题)

    Wumpus Time Limit: 2 Seconds      Memory Limit: 65536 KB One day Leon finds a very classic game call ...

  5. Poj&lpar;2225&rpar;&comma;三维BFS

    题目链接:http://poj.org/problem?id=2225 这里要注意的是,输入的是坐标x,y,z,那么这个点就是在y行,x列,z层上. 我竟然WA在了结束搜索上了,写成了输出s.step ...

  6. POJ 2777 线段树基础题

    题意: 给你一个长度为N的线段数,一开始每个树的颜色都是1,然后有2个操作. 第一个操作,将区间[a , b ]的颜色换成c. 第二个操作,输出区间[a , b ]不同颜色的总数. 直接线段树搞之.不 ...

  7. Dungeon Master POJ - 2251 &lbrack;kuangbin带你飞&rsqb;专题一 简单搜索

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

  8. POJ&period;2251 Dungeon Master (三维BFS)

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

  9. POJ 2251 Dungeon Master --- 三维BFS&lpar;用BFS求最短路&rpar;

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

随机推荐

  1. LeetCode&colon;Word Break(DP)

    题目地址:http://oj.leetcode.com/problems/word-break/ 简单的动态规划问题,采用自顶向下的备忘录方法,代码如下: class Solution { publi ...

  2. STRUTS2 标签 循环次数

    *<s:property value="menus.size()"/> * <s:if test='menus.size()>8'>    sssss ...

  3. 【动态规划】XMU 1583 Sequence

    题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1583 题目大意: T组数据,对于n(n<=6000)给定序列Xn(Xn<= ...

  4. Python之路第十天,高级&lpar;2&rpar;-多线程,多进程,协程

    线程 threading threading模块对象 描述 Thread 表示一个线程的执行对象 Lock 锁原语对象 RLock 可重入锁对象,使单线程可再次获得已经获得了的锁(递归锁定) Cond ...

  5. 用PyRestful快速构建Tornado下REST APIs 的支持

    一.安装PyRestful库 $ pip install pyrestful 二.使用案例 (一)books_service.py # -*- coding: utf-8 -*- import tor ...

  6. js获取并设置&amp&semi;lt&semi;p&amp&semi;gt&semi;&amp&semi;lt&semi;&sol;p&amp&semi;gt&semi;的显示的值。

    原文链接:http://www.nowamagic.net/librarys/posts/jquery/23 html()方法 此方法类似于JavaScript中的innerHTML属性,能够用来读取 ...

  7. Java知多少(4)J2SE、J2EE、J2ME的区别

    原文:Java知多少(4)J2SE.J2EE.J2ME的区别 1998年12月,SUN公司发布了Java 1.2,开始使用“Java 2” 这一名称,目前我们已经很少使用1.2之前的版本,所以通常所说 ...

  8. Docker集群实验环境布署--swarm【6 配置上层Nginx代理,让任意Docker client访问得到高可用的管理API】

    10.40.42.10上,也就是对应的VRRP中的10.40.42.1和2上,配置nginx tcp代理   # cat 4000_manager.venic.com_10.40.100.141-14 ...

  9. flash 右键菜单隐藏与修改

    来源:http://blog.sina.com.cn/s/blog_7264c84401014fmd.html import flash.ui.ContextMenu;import flash.ui. ...

  10. AJAX请求真的不安全么?谈谈Web安全与AJAX的关系。

    开篇三问 AJAX请求真的不安全么? AJAX请求哪里不安全? 怎么样让AJAX请求更安全? 前言 本文包含的内容较多,包括AJAX,CORS,XSS,CSRF等内容,要完整的看完并理解需要付出一定的 ...