当前位置: 首页 > news >正文

POJ 2251 Dungeon Master

对于初学搜索问题比较有挑战性的一个问题:

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!

AC Code:

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
char mp[35][35][35];
bool flag[35][35][35];
int way[6][3]={{1,0,0},{0,1,0},{0,0,1},{-1,0,0},{0,-1,0},{0,0,-1}};
int ans,judge;
int l,r,c;
struct node
{
    int x;
    int y;
    int z;
    int cnt;
};

void bfs(int x,int y,int z)
{
    node q,ne;
    queue<node> G;
    q.cnt=0;q.x=x;q.y=y;q.z=z;
    G.push(q);
    while(G.size())
    {
        q=G.front();
        G.pop();
        for(int i=0;i<6;i++)
        {
            ne.x=q.x+way[i][0];
            ne.y=q.y+way[i][1];
            ne.z=q.z+way[i][2];
            ne.cnt=q.cnt+1;

            if(ne.x<0||ne.x>=l||ne.y<0||ne.y>=r||ne.z<0||ne.z>=c||mp[ne.x][ne.y][ne.z]=='#') continue;

            if(!flag[ne.x][ne.y][ne.z])
            {
                if(mp[ne.x][ne.y][ne.z]=='E')
                {
                    ans=ne.cnt;
                    judge=1;
                    return ;
                }
                flag[ne.x][ne.y][ne.z]=true;
                G.push(ne);
            }
        }
    }
}

int main()
{
    int o,p,q;
    while(scanf("%d %d %d",&l,&r,&c)!=EOF&&l&&c&&r)
    {
        memset(mp,'#',sizeof(mp));
        memset(flag,false,sizeof(flag));
        ans=0;judge=0;

        for(int i=0;i<l;i++)
        {
            for(int j=0;j<r;j++)
            {
                for(int k=0;k<c;k++)
                {
                    cin>>mp[i][j][k];
                    if(mp[i][j][k]=='S')
                    {
                        o=i;p=j;q=k;
                    }
                }
            }
        }
        bfs(o,p,q);
        if(judge==1)  printf("Escaped in %d minute(s).\n",ans);
        else  cout<<"Trapped!"<<endl;
    }
    return 0;
}

TIP:

结构体+queue队列容器+方向数组的运用+广度优先搜索(特殊情况的排查,不重不漏)

 

转载于:https://www.cnblogs.com/myxdashuaige/p/8781881.html

相关文章:

  • 面试总结JavaScript篇
  • Generic detail view PostDetailView must be called with either an object pk or a slug.解决
  • 高端家电“金选奖”名单揭晓,激起新消费主义浪潮
  • Python2与Python3的区别
  • 集群中用Memcached来实现session共享
  • AngularJs的表单验证
  • 如何查看linux中的ssh端口开启状态
  • Go 语言之 struct 结构体
  • 安卓设置背景图平铺,同时设置背景色
  • 在GitHub多个账号上使用不同的SSH的配置方法
  • 为用户管理连接 Confluence 6 到 Jira 应用程序
  • 加密算法:DigestUtils与java MessageDigest
  • Spring Extensible XML
  • mooc-IDEA alter enter--008
  • 20172318 2017-2018-2 《程序设计与数据结构》第6周学习总结
  • [笔记] php常见简单功能及函数
  • Consul Config 使用Git做版本控制的实现
  • gops —— Go 程序诊断分析工具
  • Linux下的乱码问题
  • MySQL的数据类型
  • 分享一个自己写的基于canvas的原生js图片爆炸插件
  • 将回调地狱按在地上摩擦的Promise
  • 经典排序算法及其 Java 实现
  • 猫头鹰的深夜翻译:Java 2D Graphics, 简单的仿射变换
  • 猫头鹰的深夜翻译:JDK9 NotNullOrElse方法
  • 前端面试之闭包
  • 如何使用Mybatis第三方插件--PageHelper实现分页操作
  • 实战|智能家居行业移动应用性能分析
  • 想写好前端,先练好内功
  • k8s使用glusterfs实现动态持久化存储
  • python最赚钱的4个方向,你最心动的是哪个?
  • ​油烟净化器电源安全,保障健康餐饮生活
  • #Linux(Source Insight安装及工程建立)
  • #在线报价接单​再坚持一下 明天是真的周六.出现货 实单来谈
  • %check_box% in rails :coditions={:has_many , :through}
  • ( 10 )MySQL中的外键
  • (6)添加vue-cookie
  • (js)循环条件满足时终止循环
  • (附源码)springboot 个人网页的网站 毕业设计031623
  • (附源码)springboot教学评价 毕业设计 641310
  • (附源码)ssm高校升本考试管理系统 毕业设计 201631
  • (七)Java对象在Hibernate持久化层的状态
  • (删)Java线程同步实现一:synchronzied和wait()/notify()
  • (转) Android中ViewStub组件使用
  • (转)http协议
  • (转)从零实现3D图像引擎:(8)参数化直线与3D平面函数库
  • 、写入Shellcode到注册表上线
  • .NET 5种线程安全集合
  • .net core 6 集成和使用 mongodb
  • .NET Framework 的 bug?try-catch-when 中如果 when 语句抛出异常,程序将彻底崩溃
  • .NET 指南:抽象化实现的基类
  • .NET面试题(二)
  • .NET中的Exception处理(C#)
  • .sdf和.msp文件读取
  • [ vulhub漏洞复现篇 ] Apache APISIX 默认密钥漏洞 CVE-2020-13945