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. [BI基础] 一些不得不了解的概念

    0.Hadoop hadoop主要是用来对海量数据进行存储和计算的. 它本身是一个分布式系统,核心由分布式文件系统hdfs,和分布式计算框架mapreduce组成,在存储和计算时能够发挥出集群中每台机 ...

  2. HTML5自学笔记[ 2 ]新增表单控件和表单属性

    新增<input>属性type="email",自动验证,若输入不为邮箱,则不能提交. 新增<input>属性type="tel",在移 ...

  3. Javascript的"预编译"思考

    今天工作需要,搜索下JS面试题,看到一个题目,大约是这样的 <script> var x = 1, y = z = 0; function add(n) { n = n+1;  } y = ...

  4. 318. Maximum Product of Word Lengths ——本质:英文单词中字符是否出现可以用26bit的整数表示

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

  5. C#图书资源【更新中...】喜欢的就转存吧

    百度分享暂时取消了,需要的朋友可以直接关注我,我发给你. 重点篇 1.C#与.NET3.5高级程序设计 2.C#与.NET3.5高级程序设计.rar 3.Effective_C#_中文版_改善C#程序 ...

  6. ext afteredit

    ext afteredit详解 grid.on("afteredit",afterEidt,grid); function(obj) { obj.row;;//修改过的行从0开始 ...

  7. HDU 5773 The All-purpose Zero 求LIS

    求最长上升子序列长度: 单纯的dp时间复杂度是O(n*n)的 dp[i] = max(dp[j]+1); (0=<j<=i-1 && a[i]>a[j]) 用二分可以 ...

  8. 常用的JavaScript验证正则表达式1

    匹配Email地址的正则表达式:w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*评注:表单验证时很实用 匹配网址URL的正则表达式:[a-zA-z]+://[^s]* 评注:网 ...

  9. bzoj 2242: [SDOI2011]计算器

    #include<cstdio> #include<iostream> #include<map> #include<cmath> #define ll ...

  10. $where $options: 'g','i'

    db.classes.update({"count":{$gt:20}},{$set:{"name":"c4"}},false,false) ...