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. python 基础学习(元组,if,for)

    1.tuple对象 tuple 元组 有序的列表 tuple一旦创建不能修改 a.定义元组t=('a','b','c')空元素的tuple t=()()既表示tuple 也表示运算符的优先级 所以定义 ...

  2. jsp之 ---- 页面重定向和请求转发(笔记之深度说明)

    1.  HttpServletResponse对象的sendRedirect(String location)方法称作重定向. 如果location地址前面加上“/”,则表示  相对于Servlet容 ...

  3. 选择列表中的列无效,因为该列没有包含在聚合函数或 GROUP BY 子句中

    选择列表中的列无效,因为该列没有包含在聚合函数或 GROUP BY 子句中 T-SQL核心语句形式: SELECT     --指定要选择的列或行及其限定  [INTO ]      --INTO子句 ...

  4. CRM创建物料FM1

    这是在中联混凝土那边搞的.... method create_prd.  data: lt_return type bapiret2_tab,        ls_return like line o ...

  5. css——子代与后代选择器

    一直都以为,子代选择器与后代选择器作用是一样的,都是选择一个元素下面的子元素,直到今天才明白: 1.子代选择器(用<连接):ul>li 子选择器仅仅选择ul包围的 子元素 中的 li元素, ...

  6. 表单重置reset

    最近一直在做各种页面的“增删改查”,只是在做新增功能的时候,自己一直在使用 reset来清空form表单,原以为这样子的清空方法是万无一失的,可惜最终还是在进行“修改”操作完了之后再“新增”的时候,就 ...

  7. 328. Odd Even Linked List——多利用fake_head

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...

  8. journal

    dec 5 rpt prep exam dec 4 lie to me dec 3 exam dec 2 preparation for exam dec 1 preparation for exam ...

  9. GridControl的用法(1)

    一.属性设置 ①去除gridControl上的筛选条 //去除上面的筛选条            gridView1.OptionsView.ShowGroupPanel = false; ②设置列名 ...

  10. C#保存登录用户名供其他页面调用

    一.保存登录用户名供其他页面调用 步骤: (1)项目自带的Program.cs,类方法里定义登录的用户名为全局变量loginid,这样整个项目都可以调用它 static class Program { ...