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. 学习Linux系统永远都不晚

    作为一名机械专业毕业的学生,两年的工作经历实实在在地教会了我如何认清现实,让当初那个对机械行业无比憧憬的少年明白了自己选择的路有多艰难.由于我的父母都是工人,所以我比其他同龄人能更早地接触到工业的魅力 ...

  2. visio 2019 激活方法

    今日因工作需要使用visio,无奈下载2019版本需要激活,很多功能无法使用,最近在网上发现一个非常简单就是一个本地可执行脚本,本人已亲测完全激活成功,随分享给大家 复制下面代码: @echo off ...

  3. 智能充电安全管理首选SOC单芯片方案:SI24R2F

    SI24R2F简介:       SI24R2F是一颗工作在2.45GHZ ISM 频段,专为低功耗有源RFID应用场合设计,集成崁入式2.45GHZ 无线射频发射器模块.64次可编程NVM存储器模块 ...

  4. Python 必知的 20 个骚操作!

    以下为译文: Python 是一个解释型语言,可读性与易用性让它越来越热门. 正如 Python 之禅中所述: 优美胜于丑陋,明了胜于晦涩. 在你的日常编码中,以下技巧可以给你带来意想不到的收获.   ...

  5. 011-PHP获取数组中的元素

    <?php $monthName = array( /*定义$monthName[1]到$monthName[12]*/ 1=>"January", "Feb ...

  6. Fr3设置图片打印

    见 fr3的文件内容,为xml <?xml version="1.0" encoding="utf-8"?> <TfrxReport Vers ...

  7. ubuntu下pip的安装、升级和使用

    系统虽然自带了不同版本的python,但都没有安装pip,pyhton2.7下使用的是pip2,python3.5下使用的是pip3.下面是各自安装命令. 安装 pip2: sudo apt-get ...

  8. bzoj 4008、4011、1499

    全是扒题解,,,太弱了... 不乱BB了. 4008 #include <bits/stdc++.h> #define LL long long #define lowbit(x) x&a ...

  9. 3种python调用其他脚本的方法,你还知道其他的方法吗?

    1.用python调用python脚本 #!/usr/local/bin/python3.7 import time import os count = 0 str = ('python b.py') ...

  10. TensorFlow2 Part1:基础

    TensorFlow™是一个基于数据流编程(dataflow programming)的符号数学系统,被广泛应用于各类机器学习(machine learning)算法的编程实现,其前身是谷歌的神经网络 ...