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. (20)odoo中的action

    ---------更新时间18:06 2016-09-18 星期日15:05 2016-03-14 星期一18:07 2016-02-19 星期五---------* 窗口动作   <?xml ...

  2. Runner站立会议之个人会议(冲刺二)

    2016.5.23 今天开会确定了接下来的目标,完成收集相关数据任务 明天要寻找类型对应的按钮图标 遇到的问题:数据中男女生,有无恋爱,区域限制均可能导致计划部分有出入 2016.5.24 今天查询相 ...

  3. ubuntu14.04LS中安装SSH

    我只能说: 蛋疼了 因为 1.曾经12.04和13.10的源已经不能使用了(PS毕竟支持的时间到了) 2网上有好多说是更新源的 , 打开etc/...文件 ,然后粘贴一下他们给的源的地址 或许有些是可 ...

  4. hdu 4315 Climbing the Hill(阶梯博弈转nim博弈)

    Climbing the Hill Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  5. placehold.it-在线图片生成器(转载)

    做网站的时候 如果 有的产品等客户没有上传图片,可以用这个网站生成的图片 并配以文字进行图片的占位 以免造成页面的空挡或者页面错位等 原文地址:http://www.cnblogs.com/xumen ...

  6. 为什么html5用的jQuery Mobile在手机浏览器/微信中打开字体很小

    头部加入 <header> <metaname="viewport"content="width=device-width, initial-scale ...

  7. 深入掌握include_once与require_once的区别

    转:http://www.jb51.net/article/38587.htm  http://www.360doc.com/content/12/1022/17/7851074_243107406. ...

  8. 引用CSS文件到html网页里方法

        引用CSS文件到Html方法-css引入,css引用 使用不同的方法来引用css样式表,最终到达的效果相同,但是使用不同方法应用的css文件将影响到SEO及网页打开速度效率. html引用cs ...

  9. Dapper使用

    公司的项目使用了Dapper做数据库连接处理,感觉不错,自己研究一下怎么用. 在网上找了找资料对Dapper都比较推崇.主要是两个方面,一个是连接速度很快,一个是代码开源且简单,只有一个SqlMapp ...

  10. treap 1286郁闷的出纳员.cpp

    #include<cstdio>#include<cstdlib>#include<ctime>struct shu{ int l,r,sum,zhi,dui;}a ...