Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 20687   Accepted: 8004

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!

一个Test给你几个地图,给了你一个三维的模型,其实质就是之前人在格子里走路只能是上下左右四个方向,这回如果你在中间的地图里的话,又多了两个方向,就叫成前后吧,其实质是bfs,中规中矩的一个模式而已。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
using namespace std; int L,R,C;
char value[50][50][50];
int bushu[50][50][50]; int bfs(int i,int j,int k)
{
int flag=0; queue<int>x;
queue<int>y;
queue<int>z; x.push(i);
y.push(j);
z.push(k); while(x.size())
{
int temp1=x.front();
int temp2=y.front();
int temp3=z.front(); x.pop();
y.pop();
z.pop(); if(temp1-1>=1&&value[temp1-1][temp2][temp3]=='.'&&!bushu[temp1-1][temp2][temp3])
{
x.push(temp1-1);
y.push(temp2);
z.push(temp3);
bushu[temp1-1][temp2][temp3]=bushu[temp1][temp2][temp3]+1;
} if(temp1+1<=L&&value[temp1+1][temp2][temp3]=='.'&&!bushu[temp1+1][temp2][temp3])
{
x.push(temp1+1);
y.push(temp2);
z.push(temp3);
bushu[temp1+1][temp2][temp3]=bushu[temp1][temp2][temp3]+1;
}
if(temp2-1>=1&&value[temp1][temp2-1][temp3]=='.'&&!bushu[temp1][temp2-1][temp3])
{
x.push(temp1);
y.push(temp2-1);
z.push(temp3);
bushu[temp1][temp2-1][temp3]=bushu[temp1][temp2][temp3]+1;
}
if(temp2+1<=R&&value[temp1][temp2+1][temp3]=='.'&&!bushu[temp1][temp2+1][temp3])
{
x.push(temp1);
y.push(temp2+1);
z.push(temp3);
bushu[temp1][temp2+1][temp3]=bushu[temp1][temp2][temp3]+1;
}
if(temp3-1>=1&&value[temp1][temp2][temp3-1]=='.'&&!bushu[temp1][temp2][temp3-1])
{
x.push(temp1);
y.push(temp2);
z.push(temp3-1);
bushu[temp1][temp2][temp3-1]=bushu[temp1][temp2][temp3]+1;
}
if(temp3+1<=C&&value[temp1][temp2][temp3+1]=='.'&&!bushu[temp1][temp2][temp3+1])
{
x.push(temp1);
y.push(temp2);
z.push(temp3+1);
bushu[temp1][temp2][temp3+1]=bushu[temp1][temp2][temp3]+1;
}
if(value[temp1-1][temp2][temp3]=='E'||value[temp1+1][temp2][temp3]=='E'||value[temp1][temp2-1][temp3]=='E'||
value[temp1][temp2+1][temp3]=='E'||value[temp1][temp2][temp3-1]=='E'||value[temp1][temp2][temp3+1]=='E')
{
flag=1;
return bushu[temp1][temp2][temp3]+1;
}
}
return 0;
} void solve()
{
int i,j,k;
for(i=L;i>=1;i--)
{
for(j=R;j>=1;j--)
{
for(k=C;k>=1;k--)
{
if(value[i][j][k]=='S')
{
int temp=bfs(i,j,k);
if(temp==0)
cout<<"Trapped!"<<endl;
else
cout<<"Escaped in "<<temp<<" minute(s)."<<endl;
return;
}
}
}
}
} int main()
{
int i,j,k;
while(scanf("%d%d%d",&L,&R,&C))
{
if(L+R+C==0)
break;
for(i=1;i<=L;i++)
{
for(j=1;j<=R;j++)
{
scanf("%s",value[i][j]+1);
}
}
memset(bushu,0,sizeof(bushu));
solve();
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 2251:Dungeon Master的更多相关文章

  1. 【POJ - 2251】Dungeon Master (bfs+优先队列)

    Dungeon Master  Descriptions: You are trapped in a 3D dungeon and need to find the quickest way out! ...

  2. 【POJ 2251】Dungeon Master(bfs)

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

  3. 【一本通1248:Dungeon Master&&洛谷UVA532 Dungeon Master】

    若不会广搜转向[广搜] [题目描述] 这题是一个三维的迷宫题目,其中用‘.’表示空地,‘#’表示障碍物,‘S’表示起点,‘E’表示终点,求从起点到终点的最小移动次数,解法和二维的类似,只是在行动时除了 ...

  4. [ACM训练] 算法初级 之 搜索算法 之 深度优先算法DFS (POJ 2251+2488+3083+3009+1321)

    对于深度优先算法,第一个直观的想法是只要是要求输出最短情况的详细步骤的题目基本上都要使用深度优先来解决.比较常见的题目类型比如寻路等,可以结合相关的经典算法进行分析. 常用步骤: 第一道题目:Dung ...

  5. poj 2251 Dungeon Master

    http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

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

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

  7. POJ 2251 Dungeon Master (三维BFS)

    题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  8. BFS POJ 2251 Dungeon Master

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

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

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

随机推荐

  1. android的文件操作

    http://blog.csdn.net/fenghome/article/details/5668598 android的文件操作要有权限: <uses-permission android: ...

  2. 008-PHP定义数组

    <?php /*定义数组$Cities[]*/ $Cities[0] = "北京"; $Cities[1] = "天津"; $Cities[2] = &q ...

  3. 【LOJ6498】「雅礼集训 2018 Day2」农民

    题面 solution 直接暴力模拟,原数据可获得满分的成绩. 对于每个点,其父亲对其都有一个限制.故我们只需要判断当前点到根的路径上的限制是否都能满足即可. 考虑用树剖+线段树维护这个限制.考虑到翻 ...

  4. Java8集合框架——HashSet源码分析

    本文的目录结构: 一.HashSet 的 Javadoc 文档注释和简要说明 二.HashSet 的内部实现:内部属性和构造函数 三.HashSet 的 add 操作和扩容 四.HashSet 的 r ...

  5. oracle查询SQL优化相当重要

    如果表中的时间字段是索引,那么时间字段不要使用函数,函数会使索引失效. 例如: select * from mytable where trunc(createtime)=trunc(sysdate) ...

  6. 洛谷 三月月赛 A

    模拟模拟 #include<bits/stdc++.h> using namespace std; inline int ra() { ,f=; char ch=getchar(); ; ...

  7. JPA#OneToOne

    无力吐槽. 一对一,一个人有一个身份证号码.一个人有一条命,类似于这一种的就是一对一的关系. 涉及到的注解两个: OneToOne JoinColumn( name="当前实体对应数据库表中 ...

  8. TensorFlow2 Part2:基础知识回顾

    python面向对象编程回顾 基础概念: 面向对象的编程简称OOP,它把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 面向对象的设计思想是抽象出Class,根据Class(类)创建In ...

  9. oracle数据泵导出导入

    先创建一个目录:比如 Create  or Replace directory  DATA_PUMP_DIR as 'D:\DataPipe';   然后给导入导出的用户赋权限: Grant read ...

  10. 八、React实战:可交互待办事务表(表单使用、数据的本地缓存local srtorage、生命同期函数(页面加载就会执行函数名固定为componentDidMount()))

    一.项目功能概述 示例网址:http://www.todolist.cn/ 功能: 输入待做事项,回车,把任务添加到 [正在进行] [正在进行] 任务,勾选之后,变成已[经完成事项] [已完成事务], ...