题目链接:http://poj.org/problem?id=2251

Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 37296   Accepted: 14266

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

题解:

三维的纯BFS。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; struct node
{
int x, y, z, step;
}; int L, R, C;
char m[MAXN][MAXN][MAXN];
int vis[MAXN][MAXN][MAXN];
int dir[][] = { {,,},{,,},{,,},{-,,},{,-,},{,,-} }; queue<node>que;
int bfs(node s, node e)
{
ms(vis,);
while(!que.empty()) que.pop(); s.step = ;
vis[s.x][s.y][s.z] = ;
que.push(s); while(!que.empty())
{
node now = que.front();
que.pop(); if(now.x==e.x && now.y==e.y && now.z==e.z)
return now.step; for(int i = ; i<; i++)
{
node tmp;
tmp.x = now.x + dir[i][];
tmp.y = now.y + dir[i][];
tmp.z = now.z + dir[i][];
if(tmp.x>= && tmp.x<=L && tmp.y>= && tmp.y<=R && tmp.z>= && tmp.z<=C
&& m[tmp.x][tmp.y][tmp.z]!='#' && !vis[tmp.x][tmp.y][tmp.z] )
{
vis[tmp.x][tmp.y][tmp.z] = ;
tmp.step = now.step + ;
que.push(tmp);
}
}
}
return -;
} int main()
{
while(scanf("%d%d%d",&L, &R, &C) && (L||R||C))
{
for(int i = ; i<=L; i++)
for(int j = ; j<=R; j++)
scanf("%s", m[i][j]+); node s, e;
for(int i = ; i<=L; i++)
for(int j = ; j<=R; j++)
for(int k = ; k<=C; k++)
{
if(m[i][j][k]=='S') s.x = i, s.y = j, s.z = k;
if(m[i][j][k]=='E') e.x = i, e.y = j, e.z = k;
} int ans = bfs(s,e);
if(ans==-)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n", ans);
}
return ;
}

POJ2251 Dungeon Master —— BFS的更多相关文章

  1. POJ2251 Dungeon Master(bfs)

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

  2. BFS POJ2251 Dungeon Master

    B - Dungeon Master Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  3. hdu 2251 Dungeon Master bfs

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17555   Accepted: 6835 D ...

  4. 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 ...

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

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

  6. Dungeon Master bfs

    time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u POJ 2251 Descriptio ...

  7. 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 ...

  8. [poj] Dungeon Master bfs

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

  9. poj 2251 Dungeon Master( bfs )

    题目:http://poj.org/problem?id=2251 简单三维 bfs不解释, 1A,     上代码 #include <iostream> #include<cst ...

随机推荐

  1. hzwer与逆序对

    codevs——4163 hzwer与逆序对 貌似这个题和上个题是一样的((⊙o⊙)…)  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题解       题目 ...

  2. Jquery表单序列化和json操作

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 【java】java base64编码与解码

    参考地址:http://blog.csdn.net/zhou_kapenter/article/details/62890262 要求:JDK1.8+ 使用java原生工具类即可实现 [这里展示字符串 ...

  4. 制作ubuntu U盘安装盘

    sudo dd if=ubuntu.iso of=/dev/sdb2 sudo syslinux /dev/sdb1

  5. SpringCloud中Redis的使用

    1.引入redis相关jar包 <dependency> <groupId>org.springframework.boot</groupId> <artif ...

  6. 使用RPi-Monitor监控、统计Guitar的运行状态

    前言 之前发在ickey社区上的一系列文章: 犹抱琵琶半遮面,无人知是荔枝来--unboxing & interview 一.二.三 葡萄美酒夜光杯,巧妇难为无米炊--资料与社区 一支穿云箭, ...

  7. linux 进程间通信之 消息队列

    消息队列就是一个消息的链表. 能够把消息看作一个记录,具有特定的格式以及特定的优先级.对消息队列有写权限的进程能够向中依照一定的规则加入新消息.有读权限的进程则能够读走消息. 读走就没有了.消息队列是 ...

  8. ios9定位服务的app进入后台三分钟收不到经纬度,应用被挂起问题及解决方式

    原来定位服务是10分钟收不到定位信息就挂起定位,如今变为最短3分钟,预计都是为了省电吧. 仅仅要你开启应用的后台定位,而且10分钟有一次定位,那么苹果就不会关闭你的线程.如今变成3分钟.若你的应用开启 ...

  9. 【转载】细聊分布式ID生成方法

    一.需求缘起 几乎所有的业务系统,都有生成一个记录标识的需求,例如: (1)消息标识:message-id (2)订单标识:order-id (3)帖子标识:tiezi-id 这个记录标识往往就是数据 ...

  10. 聊聊高并发(二十四)解析java.util.concurrent各个组件(六) 深入理解AQS(四)

    近期总体过了下AQS的结构.也在网上看了一些讲AQS的文章,大部分的文章都是泛泛而谈.又一次看了下AQS的代码,把一些新的要点拿出来说一说. AQS是一个管程.提供了一个主要的同步器的能力,包括了一个 ...