Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 24311   Accepted: 9425

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!
题目链接:poj2251

/*题目大意:l,r,c。表示l层,r行,c列。从起点s到终点e的最小步数、其中.为通道,#为墙。只能上下左右走,也能从上一层跳到相对应的下一层
算法分析:三维bfs搜索
坑点:每次处理完后要清空队列中所有元素
*/ #include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <queue>
using namespace std; int L, R, C;
int vis[40][40][40];
int l[6] = {1, -1, 0, 0, 0, 0}, r[6] = {0, 0, 1, -1, 0, 0}, c[6] = {0, 0, 0, 0, 1, -1};
char map[40][40][40];
struct node {
int z, x, y;
int time;
}; node start, end;
queue <node> q; int judge(int a, int b, int c) {
if (a>=0 && a<L && b>=0 && b<R && c>=0 && c<C) return 1;
return 0;
} int dfs() { while (!q.empty()) {
node cur, next;
cur = q.front();
q.pop();
if (cur.z == end.z && cur.x == end.x && cur.y == end.y && judge(cur.z,cur.x,cur.y)) return cur.time;
for (int i = 0; i<6; i++) {
next.z = cur.z + l[i];
next.x = cur.x + r[i];
next.y = cur.y + c[i];
next.time = cur.time + 1;
if (judge(next.z, next.x, next.y) && map[next.z][next.x][next.y] != '#' && !vis[next.z][next.x][next.y]) {
if (next.z == end.z && next.x == end.x && next.y == end.y) return next.time;
q.push(next);
vis[next.z][next.x][next.y] = 1;
}
}
}
return -1;
} int main() {
while (cin >> L >> R >> C && (L+R+C)) {
memset(map, 0, sizeof(map));
memset(vis, 0, sizeof(vis)); for (int i = 0; i<L; i++) {
for (int j = 0; j<R; j++) {
for (int k = 0; k<C; k++) {
cin >> map[i][j][k];
if (map[i][j][k] == 'S') {
start.z = i;
start.x = j;
start.y = k;
start.time = 0;
vis[i][j][k] = 1;
q.push(start);
}
else if (map[i][j][k] == 'E') {
end.z = i;
end.x = j;
end.y = k;
}
}
}
}
int ans = dfs();
if (ans == -1) cout << "Trapped!" << endl;
else cout << "Escaped in " << ans << " minute(s)."<< endl;
while (!q.empty()) q.pop();
}
return 0;
}

poj_2251的更多相关文章

随机推荐

  1. xamarin android menu的用法

    在Android中的菜单有如下几种: OptionMenu:选项菜单,android中最常见的菜单,通过Menu键来调用 SubMenu:子菜单,android中点击子菜单将弹出一个显示子菜单项的悬浮 ...

  2. xamarin android alertdialog详解

    说明一下:学习xamarin android一段时间,准备写一些xamarin android相关的例子,alertdialog也是使用的非常多得空间之一,非常感谢鸟巢上的小猪,我也是看着他写的教程学 ...

  3. XML文件解析数据结构

    最近在解析Android安装包内经过编译的二进制XML文件时想在内存中建立起其对应的树结构. 想了一早晨,思路如下图. 多叉树中的每个节点除了有子节点和兄弟节点以外还有一个指针指向父节点,然后根据状态 ...

  4. 走进Vue时代进阶篇(01):重构电商购物车模块

    前言 从这篇文章开始,我准备给大家分享一些关于Vue.js这门框架的技巧性系列文章,正好我们公司项目中也用到了Vue.所以,教是最好的学.进阶篇比较适合于二三线城市,还在小厂打拼的童鞋们.欢迎你们跟着 ...

  5. C#设计模式之二十三解释器模式(Interpreter Pattern)【行为型】

    一.引言   今天我们开始讲"行为型"设计模式的第十一个模式,也是面向对象设计模式的最后一个模式,先要说明一下,其实这个模式不是最后一个模式(按Gof的排序来讲),为什么把它放在最 ...

  6. Java贪吃蛇感想

    暑假敲代码的效率真的不高呀,一个这种小游戏从最开始构思到最后实施代码,从最开始的Demo版本到最后的第四版本,花了一个星期了.本想再更新一个版本,加入双人对战模式,还想写个AI版,可是我估计按照现在我 ...

  7. ul li自适应居中导航

    <BODY> <div class="box"> <ul class="nav"> <li>邮箱管理</l ...

  8. SQL Server 禁用扩展存储过程

    概述 扩展存储过程是 SQL Server 实例可以动态加载和运行的 DLL.扩展存储过程是使用 SQL Server 扩展存储过程 API 编写的,可直接在 SQL Server 实例的地址空间中运 ...

  9. mysql启动日志文件log_bin

    今天正在无所事事的时候,突然收到需要我打开mysql的log_bin,当时我就懵逼了...不多说别的,我连这个log_bin在哪里,怎么知道是否启动了都不知道,怎么去做? 在万分纠结下,查询了很多资料 ...

  10. JAVA定时任务调度之Timer入门详解(一)

    所谓的Timer,打开jdk的api文档可以看到它的定义:一种工具,线程用其安排以后在后台线程中执行的任务.可安排任务执行一次,或者定期重复执行.通俗点讲就是说:有且仅有一个后台线程对多个业务线程进行 ...