Dungeon Master

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 18   Accepted Submission(s) : 12
Problem 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
PKU
 
 
 
又一道BFS水题,我发现我刷搜索水题很溜啊哈哈。。
 
 
#include<iostream>
#include<cstring>
using namespace std;
char cube[31][31][31];
bool sign[31][31][31];
int L,R,C; struct pos
{
int l,r,c;
}; pos que[54000],beg;
int step[54000];
int BFS()
{
int front=0,rear=1;
que[0]=beg;
sign[que[0].l][que[0].r][que[0].c]=true;
while(front<rear)
{
if(que[front].l-1>=0&&!sign[que[front].l-1][que[front].r][que[front].c]&&cube[que[front].l-1][que[front].r][que[front].c]!='#')
{
que[rear]=que[front];
que[rear].l=que[front].l-1;
sign[que[rear].l][que[rear].r][que[rear].c]=true;
step[rear]=step[front]+1;
if(cube[que[rear].l][que[rear].r][que[rear].c]=='E')
return step[rear];
rear++;
}
if(que[front].l+1<L&&!sign[que[front].l+1][que[front].r][que[front].c]&&cube[que[front].l+1][que[front].r][que[front].c]!='#')
{
que[rear]=que[front];
que[rear].l=que[front].l+1;
sign[que[rear].l][que[rear].r][que[rear].c]=true;
step[rear]=step[front]+1;
if(cube[que[rear].l][que[rear].r][que[rear].c]=='E')
return step[rear];
rear++;
}
if(que[front].r-1>=0&&!sign[que[front].l][que[front].r-1][que[front].c]&&cube[que[front].l][que[front].r-1][que[front].c]!='#')
{
que[rear]=que[front];
que[rear].r=que[front].r-1;
sign[que[rear].l][que[rear].r][que[rear].c]=true;
step[rear]=step[front]+1;
if(cube[que[rear].l][que[rear].r][que[rear].c]=='E')
return step[rear];
rear++;
}
if(que[front].r+1<R&&!sign[que[front].l][que[front].r+1][que[front].c]&&cube[que[front].l][que[front].r+1][que[front].c]!='#')
{
que[rear]=que[front];
que[rear].r=que[front].r+1;
sign[que[rear].l][que[rear].r][que[rear].c]=true;
step[rear]=step[front]+1;
if(cube[que[rear].l][que[rear].r][que[rear].c]=='E')
return step[rear];
rear++;
}
if(que[front].c-1>=0&&!sign[que[front].l][que[front].r][que[front].c-1]&&cube[que[front].l][que[front].r][que[front].c-1]!='#')
{
que[rear]=que[front];
que[rear].c=que[front].c-1;
sign[que[rear].l][que[rear].r][que[rear].c]=true;
step[rear]=step[front]+1;
if(cube[que[rear].l][que[rear].r][que[rear].c]=='E')
return step[rear];
rear++;
}
if(que[front].c+1<C&&!sign[que[front].l][que[front].r][que[front].c+1]&&cube[que[front].l][que[front].r][que[front].c+1]!='#')
{
que[rear]=que[front];
que[rear].c=que[front].c+1;
sign[que[rear].l][que[rear].r][que[rear].c]=true;
step[rear]=step[front]+1;
if(cube[que[rear].l][que[rear].r][que[rear].c]=='E')
return step[rear];
rear++;
}
front++;
}
return -1;
} int main()
{
while(cin>>L>>R>>C&&(R+C+L))
{
int i,j,k;
memset(sign,false,sizeof(sign));
memset(step,0,sizeof(step));
for(i=0;i<L;i++)
{
for(j=0;j<R;j++)
for(k=0;k<C;k++)
{
cin>>cube[i][j][k];
if(cube[i][j][k]=='S')
{
beg.l=i;
beg.r=j;
beg.c=k;
}
}
}
int ans=BFS();
if(ans==-1)
cout<<"Trapped!"<<endl;
else
cout<<"Escaped in "<<ans<<" minute(s)."<<endl; }
}
 

HDOJ-三部曲一(搜索、数学)-1005-Dungeon Master的更多相关文章

  1. Dungeon Master 分类: 搜索 POJ 2015-08-09 14:25 4人阅读 评论(0) 收藏

    Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20995 Accepted: 8150 Descr ...

  2. POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索)

    POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索) Description You ar ...

  3. Dungeon Master POJ - 2251 (搜索)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 48605   Accepted: 18339 ...

  4. Dungeon Master hdoj

    Dungeon Master Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Tot ...

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

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

  6. UVa532 Dungeon Master 三维迷宫

        学习点: scanf可以自动过滤空行 搜索时要先判断是否越界(L R C),再判断其他条件是否满足 bfs搜索时可以在入口处(push时)判断是否达到目标,也可以在出口处(pop时)   #i ...

  7. POJ 2251 Dungeon Master【三维BFS模板】

    Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45743 Accepted: 17256 Desc ...

  8. BFS POJ 2251 Dungeon Master

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

  9. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  10. poj 2251 Dungeon Master

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

随机推荐

  1. HTML5自学笔记[ 18 ]canvas绘图基础5

    获取图像数据:getImgData(x,y,w,h),返回的是一个ImageData对象,这个对象有三个属性保存图像信息:width/height/data.data是一个数组,保存了每个像素的信息, ...

  2. iOS应用架构现状分析

    iOS从2007年诞生至今已有近10年的历史,10年的时间对iOS技术圈来说足够产生相当可观的沉淀,尤其这几年的技术分享氛围无论国内国外都显得异常活跃.本文就iOS架构这一主题,结合开发圈里讨论较多的 ...

  3. 玩转linux文件(重点)

    一.几个主要的操作 mkdir—创建目录 cp—复制文件和目录 mv——移动/重命名文件和目录 rm——删除文件和目录 ln——创建硬链接和软链接 二.几个考点: 通配符 硬链接和软链接(符号链接) ...

  4. 转载Javascript继承两种形式详解

    一直想对Javascript再次做一些总结,正好最近自己写了一个小型Js UI库,总结了一下Js的继承机制,在网上也看了一些前辈们博客里的总结,感觉分析不是特别全面.这里仅仅是把自己的学习体会拿出来分 ...

  5. PHP函数——urlencode() 函数

    urlencode($str)的作用是对字符串$str进行url编码,方便$str作为一个变量传递给下一页,一般情况下$str有两种, 第一种是数组类型,如果想将数组作为url的一个参数,即必须将数组 ...

  6. 用js实现返回上一页

    <a href="javascript :;" onClick="javascript :history.back(-1);">返回上一页</ ...

  7. Oozie简介

    在Hadoop中执行的任务有时候需要把多个Map/Reduce作业连接到一起,这样才能够达到目的.[1]在Hadoop生态圈中,有一种相对比较新的组件叫做Oozie[2],它让我们可以把多个Map/R ...

  8. mysql用户备份与修复

    1.修复表repair table tb1 [use frm]; #红色部分代表可添加也可不加, 2.show variables like '%timeout%';  #查询关键字 3. 更改数据, ...

  9. POJ 2240 && ZOJ 1082 Arbitrage 最短路,c++ stl pass g++ tle 难度:0

    http://poj.org/problem?id=2240 用log化乘法为加法找正圈 c++ 110ms,g++tle #include <string> #include <m ...

  10. codeigniter在nginx 下支持pathinfo和去除index.php的方法

    as今天准备把网站搬迁到nginx上发现codeigniter框架在nginx上不能使用,后来发现是nginx不支持pathinfo,下面介绍怎么在nginx下开启pathinfo 开始pathinf ...