B - Dungeon Master

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status

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!

BFS水题,套模板按6个方向拓展就行了。

 #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <string.h>
using namespace std;
int to[][] = {{,,},{,,-},{,,},{,-,},{,,},{-,,}};
char s[][][];
int maps[][][];
int x1,y1,z1;
int x2,y2,z2;
int L,R,C;
struct data
{
int x;
int y;
int z;
int step;
};
int check(int x,int y,int z)
{
if(x< || y< || z< || x>=L || y>=R || z>=C)
return ;
else if(s[x][y][z] == '#')
return ;
else if(maps[x][y][z])
return ;
return ;
}
int bfs()
{
queue<data> q;
data now,nex;
now.x=x1;
now.y=y1;
now.z=z1;
now.step=;
maps[x1][y1][z1]=;
q.push(now);
while(!q.empty())
{
now=q.front();
q.pop();
if(now.x==x2&&now.y==y2&&now.z==z2)
{
return now.step;
}
for(int i=;i<;i++)
{ nex=now;
nex.x=now.x+to[i][];
nex.y=now.y+to[i][];
nex.z=now.z+to[i][];
if(check(nex.x,nex.y,nex.z))
{
continue;
}
maps[nex.x][nex.y][nex.z]=;
nex.step=now.step+;
q.push(nex);
}
}
return ;
}
int main()
{
while(scanf("%d%d%d",&L,&R,&C),L+R+C)
{
cin>>R>>C;
int i,j,k;
for(i=;i<L;i++) //
{
for(j=;j<R;j++)
{
scanf("%s",s[i][j]);
for(k=;k<C;k++)
{
if(s[i][j][k]=='S')
{
x1=i;
y1=j;
z1=k;
}
else if(s[i][j][k]=='E')
{
x2=i;
y2=j;
z2=k;
}
}
}
}
memset(maps,,sizeof(maps));
int ans;
ans=bfs();
if(ans)
{printf("Escaped in %d minute(s).\n",ans);}
else
{
printf("Trapped!\n");
}
}
return ;
}

BFS POJ2251 Dungeon Master的更多相关文章

  1. POJ2251 Dungeon Master —— BFS

    题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  2. POJ-2251 Dungeon Master (BFS模板题)

    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...

  3. POJ2251 Dungeon Master(bfs)

    题目链接. 题目大意: 三维迷宫,搜索从s到e的最小步骤数. 分析: #include <iostream> #include <cstdio> #include <cs ...

  4. POJ2251——Dungeon Master(三维BFS)

    和迷宫问题区别不大,相比于POJ1321的棋盘问题,这里的BFS是三维的,即从4个方向变为6个方向. 用上队列的进出操作较为轻松. #include<iostream> #include& ...

  5. bfs—Dungeon Master—poj2251

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32228   Accepted: 12378 ...

  6. 【POJ - 2251】Dungeon Master (bfs+优先队列)

    Dungeon Master  Descriptions: You are trapped in a 3D dungeon and need to find the quickest way out! ...

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

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

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

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

  9. POJ 2251 Dungeon Master (非三维bfs)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 55224   Accepted: 20493 ...

随机推荐

  1. 和spring cloud/boot 学习如何管理自己的组件

    案例, 功能: 需要写一个往kafka上报数据的组建. 当组建启动时,需要建立如下资源: 1, 和kafka建立若干条连接 2, 启动一个线程池 3, 启动上报一个缓冲区 问题如下: 1, 如何在sp ...

  2. 洛谷 P3674 小清新人渣的本愿 [莫队 bitset]

    传送门 题意: 给你一个序列a,长度为n,有Q次操作,每次询问一个区间是否可以选出两个数它们的差为x,或者询问一个区间是否可以选出两个数它们的和为x,或者询问一个区间是否可以选出两个数它们的乘积为x ...

  3. BZOJ 2809: [Apio2012]dispatching [主席树 DFS序]

    传送门 题意:查询树上根节点值*子树中权值和$\le m$的最大数量 最大值是多少 求$DFS$序,然后变成区间中和$\le m$最多有几个元素,建主席树,然后权值线段树上二分就行了 $WA$:又把边 ...

  4. BZOJ 2806: [Ctsc2012]Cheat [广义后缀自动机 单调队列优化DP 二分]

    2806: [Ctsc2012]Cheat 题意: 多个主串和多个询问串,每次询问将询问串分成多个连续子串,如果一个子串长度>=L且在主串中出现过就是熟悉的 如果熟悉的字符串长度>=询问串 ...

  5. Orleans例子源码

    这是Orleans系列文章中的一篇.首篇文章在此 我共享以下我现在写教程的简单的Orleans例子源码. 这个代码已经是我为了写word改动过了的.不过大体内容是通用的. 我写博客总体想法是:去除所有 ...

  6. memcached 与 redis 的区别和具体应用场景

    1. Memcached简介 Memcached是以LiveJurnal旗下Danga Interactive公司的Bard Fitzpatric为首开发的高性能分布式内存缓存服务器.其本质上就是一个 ...

  7. LeetCode - 690. Employee Importance

    You are given a data structure of employee information, which includes the employee's unique id, his ...

  8. ionic2 +Angular 使用自定义图标

    结合阿里巴巴矢量图标库实现在ionic2开发中使用自定义图标. step1:在阿里巴巴图标管理中新建项目,并添加自己选中的图标到购物车: step2:将购物车中的图标"添加至项目" ...

  9. TKCPP

    volume one: http://book.huihoo.com/thinking-in-cpp-2nd-ed-vol-one/ volume2 : http://book.huihoo.com/ ...

  10. 基于queryperf 和 perftcpdns 的DNS压力测试

    最近在AWS上安装了PPTP VPN 做代理,手机, pad 也可以无缝FQ,甚是开心.最近工作不太忙,研究一下缓存加速的调优.系统已经安装的nscd文件级的缓存和dnsmasq,cpu级的dns缓存 ...