poj 2251 Dungeon Master(BFS)

简介:
Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 21394   Accepted: 8312

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

题目大意:首先给你三个数l,r,c;分别表示有l个平面,r行,c列,我们需要从 S 走到 E ,可以通过6个方向行走(即上下东西南北),看是否能够走到,如果能的话,输出最小的分钟数;否则输出Trapped!;

解题思路:首先声明一点,如果遇到最短路的话,首先考虑的是BFS,然后就是固定的套路了。。。

上代码:

/*
Date : 2015-09-08 晚上

Author : ITAK

Motto :

今日的我要超越昨日的我,明日的我要胜过今日的我;
以创作出更好的代码为目标,不断地超越自己。
*/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

///最短路,用DFS一定很难做出来,一定要BFS

const int maxn = 30000;

struct Point
{
    int x, y, z;
}q[maxn];

int len[maxn];
int xx[6] = {-1,1,0,0,0,0};
int yy[6] = {0,0,-1,1,0,0};
int zz[6] = {0,0,0,0,-1,1};
bool vis[35][35][35];
char map[35][35][35];
int l,r,c,sx,sy,sz;

int bfs()
{
    int rear, front, dx, dy, dz;
    memset(vis, false, sizeof(vis));
    memset(len, 0, sizeof(len));
    q[0].x=sx, q[0].y=sy, q[0].z=sz;
    front = rear = 0;
    while(front <= rear)
    {
        for(int i=0; i<6; i++)
        {
            dx = q[front].x + xx[i];
            dy = q[front].y + yy[i];
            dz = q[front].z + zz[i];
            if(!vis[dx][dy][dz] && (map[dx][dy][dz]=='.'||map[dx][dy][dz]=='E')
               && dx>=0 && dx<l && dy>=0 && dy<r && dz>=0 && dz<c)
               {
                 vis[dx][dy][dz] = 1;
                 rear++;
                 q[rear].x = dx;
                 q[rear].y = dy;
                 q[rear].z = dz;
                 len[rear] = len[front]+1;
                 if(map[dx][dy][dz] == 'E')
                    return len[rear];
               }
        }
        front++;
    }
    return 0;
}
int main()
{
     while(cin>>l>>r>>c)
     {
         if(l==0 && r==0 && c==0)
            break;
         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')
                        sx=i, sy=j, sz=k;
                 }
             }
         }
         int ret = bfs();
         if(ret)
            printf("Escaped in %d minute(s).\n",ret);
         else
            puts("Trapped!");
     }
    return 0;
}


目录
相关文章
|
Ubuntu 编译器 C++
【Conan 入门教程 】在Ubuntu上使用Conan编译C++第三方库:一站式解决方案
【Conan 入门教程 】在Ubuntu上使用Conan编译C++第三方库:一站式解决方案
4007 1
|
Web App开发 应用服务中间件 nginx
Windows使用Nginx搭建RTMP服务器
简介 Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。 nginx-rmtp-module是Nginx服务器的流媒体插件。
5151 0
|
存储 传感器 网络协议
通信协议缓冲区管理全景:TCP、UDP、ZMQ、DBus、SSL、SOME/IP通讯协议的缓冲区解析...
通信协议缓冲区管理全景:TCP、UDP、ZMQ、DBus、SSL、SOME/IP通讯协议的缓冲区解析...
814 0
|
NoSQL Ubuntu Linux
CTF-Pwn 入门:环境搭建
CTF-Pwn 入门:环境搭建
|
缓存 前端开发 JavaScript
|
数据采集 存储 NoSQL
实现网页认证:使用Scrapy-Selenium处理登录
在网络爬虫的世界中,我们经常需要面对一些需要用户认证的网页,如登录、注册验证等。本文将介绍如何使用Scrapy-Selenium来处理这类网页,实现自动化登录和爬取。
860 0
实现网页认证:使用Scrapy-Selenium处理登录
|
Ubuntu Linux PyTorch
WSL2安装历程
WSL2安装历程
904 1
WSL2安装历程
|
存储 SQL Java
Mybatis:Example类的使用--基本增删改查,模糊查询,排序,or,分页查询
Mybatis:Example类的使用--基本增删改查,模糊查询,排序,or,分页查询
Mybatis:Example类的使用--基本增删改查,模糊查询,排序,or,分页查询
|
程序员 Python
782.【技术】超详细的requests离线安装指南!!!!让你少走弯路!!
782.【技术】超详细的requests离线安装指南!!!!让你少走弯路!!
983 0
《ANTLR 4权威指南 》一2.5 语法分析树监听器和访问器
ANTLR的运行库提供了两种遍历树的机制。默认情况下,ANTLR使用内建的遍历器访问生成的语法分析树,并为每个遍历时可能触发的事件生成一个语法分析树监听器接口(parse-tree listener interface)。监听器非常类似于XML解析器生成的SAX文档对象。
4532 0

热门文章

最新文章

下一篇
开通oss服务