POJ2251(BFS)

POJ2251-Dungeon Master 北大POJ2251-Dungeon Master 解题报告+AC代码 立即下载

POJ2251(BFS)


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!
题意

你被困在一个3D地牢,需要找到最快的出路!地牢是由单位立方体组成,可以或不可以用岩石填充。将一个单位向北,南,东,西,上或下移动需要一分钟。你不能对角地移动,迷宫被四周的坚实岩石包围。

是逃生可能吗?如果是,需要多长时间?

思路

​ 用BFS是搜索迷宫的三维版本。

代码
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class DungeonMaster {
  int l;//层数
  int r;//每一层的行
  int c;//每一层的列
  char[][][] map;

  int[][][] vis;

  int[][] dir = {{0, 0, -1}, {0, 0, 1}, {1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}};//搜索的6个方位

  List<Node> queue = new ArrayList<Node>();

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    DungeonMaster dungeonMaster;
    while (sc.hasNext()) {
      dungeonMaster = new DungeonMaster();
      dungeonMaster.l = sc.nextInt();//层数
      dungeonMaster.r = sc.nextInt();//每一层的行
      dungeonMaster.c = sc.nextInt();//每一层的列
      int startL = 0;
      int startR = 0;
      int startC = 0;
      int endL = 0;
      int endR = 0;
      int endC = 0;
      if (dungeonMaster.l == 0 && dungeonMaster.r == 0 && dungeonMaster.c == 0) {
        break;
      }

      dungeonMaster.map = new char[dungeonMaster.l][dungeonMaster.r][dungeonMaster.c];
      dungeonMaster.vis = new int[dungeonMaster.l][dungeonMaster.r][dungeonMaster.c];
      for (int i = 0; i < dungeonMaster.l; i++) {//层数
        for (int j = 0; j < dungeonMaster.r; j++) {//每一层的行
          String str = sc.next();
          for (int k = 0; k < dungeonMaster.c; k++) {//每一层的列
            dungeonMaster.map[i][j][k] = str.charAt(k);
            dungeonMaster.vis[i][j][k] = 0;
            if (str.charAt(k) == 'S') {
              startL = i;
              startR = j;
              startC = k;
            }
            if (str.charAt(k) == 'E') {
              endL = i;
              endR = j;
              endC = k;
            }
          }
        }
      }

      int result = dungeonMaster.bfs(startL, startR, startC, endL, endR, endC);
      if (result != -1) {
        System.out.println("Escaped in " + result + " minute(s).");
      } else {
        System.out.println("Trapped!");
      }
    }
  }

  public int bfs(int startL, int startR, int startC, int endL, int endR, int endC) {
    Node start_node = new Node();
    start_node.setL(startL);
    start_node.setR(startR);
    start_node.setC(startC);
    start_node.setDis(0);
    vis[startL][startR][startC] = 1;
    queue.add(start_node);
    int front = 0;
    int rear = 1;
    while (front < rear) {
      Node now_node = queue.get(front);
      front++;
      for (int i = 0; i < 6; i++) {
        Node next = new Node();
        next.setL(now_node.getL() + dir[i][0]);
        next.setR(now_node.getR() + dir[i][1]);
        next.setC(now_node.getC() + dir[i][2]);
        next.setDis(now_node.getDis() + 1);
        if (next.getR() >= 0 && next.getL() >= 0 && next.getC() >= 0 && next.getC() < c && next.getL() < l && next.getR() < r//在迷宫内
            && map[next.getL()][next.getR()][next.getC()] != '#' && vis[next.getL()][next.getR()][next.getC()] == 0) {//还没有访问过且不是墙壁
          if (map[next.getL()][next.getR()][next.getC()] == 'E') {
            return next.getDis();
          }
          queue.add(next);
          rear++;
        }
      }
    }
    return -1;
  }

  static class Node {
    private int l;
    private int r;
    private int c;
    private int dis;
    public void setDis(int dis) {
      this.dis = dis;
    }
    public int getDis() {
      return dis;
    }
    public int getR() {
      return r;
    }
    public int getL() {
      return l;
    }
    public int getC() {
      return c;
    }
    public void setL(int l) {
      this.l = l;
    }
    public void setR(int r) {
      this.r = r;
    }
    public void setC(int c) {
      this.c = c;
    }
  }
}

Java代码超出内存限制。。。

#include <iostream>
#include <cstdio>
#include <string.h>
#include <queue>
using namespace std;
int m, n, h;
const int maxn=33;
char s[maxn][maxn][maxn];
int vis[maxn][maxn][maxn];
int dis[maxn][maxn][maxn];
struct node{
  int x, y, z;
  node(int xx, int yy, int zz)
  {
    x=xx;
    y=yy;
    z=zz;
  }
};
int cm[6]={1,0,-1,0,0,0};
int cn[6]={0,1,0,-1,0,0};
int ch[6]={0,0,0,0,1,-1};
node S=node(0, 0, 0), E=node(0, 0, 0);


int bfs()
{
  queue<node>que;
  que.push(S);
  vis[S.x][S.y][S.z]=1;
  while(!que.empty())
  {
    node no=que.front();
    que.pop();
    int x=no.x, y=no.y, z=no.z;
    for(int i=0; i<6; i++)
    {
      int nx=x+cm[i], ny=y+cn[i], nz=z+ch[i];
      if(nx==E.x&&ny==E.y&&nz==E.z)
      {
        return dis[x][y][z]+1;
      }
      if(nx>=0&&nx<m&&ny>=0&&ny<n&&nz>=0&&nz<h&&s[nx][ny][nz]=='.'&&!vis[nx][ny][nz])
      {
        vis[nx][ny][nz]=1;
        que.push(node(nx, ny, nz));
        dis[nx][ny][nz]=dis[x][y][z]+1;
      }
    }
  }
  return -1;
}



int main(void)
{
  while(scanf("%d%d%d", &m, &n, &h)&&m&&n&&h)
  {
    memset(dis, 0, sizeof(dis));
    memset(vis, 0, sizeof(vis));
    for(int i=0; i<m; i++)
    {
      for(int j=0; j<n; j++)
      {
        scanf("%s", s[i][j]);
      }
    }
    for(int i=0; i<m; i++)
    {
      for(int j=0; j<n; j++)
      {
        for(int k=0; k<h; k++)
        {
          if(s[i][j][k]=='S')
            S=node(i, j, k);
          if(s[i][j][k]=='E')
            E=node(i, j, k);
        }
      }
    }

    int res=bfs();
    if(res==-1)printf("Trapped!\n");
    else printf("Escaped in %d minute(s).\n", res);
  }
  return 0;
}
POJ第2251题 北京大学Online judge 第2251题 POJ2251 立即下载

相关推荐

保姆级避坑指南:在Ubuntu 18.04上搞定速腾Helios雷达驱动与fast-LIO2的完整配置流程

本文提供了一份详细的速腾Helios雷达与fast-LIO2在Ubuntu 18.04上的配置避坑指南,涵盖驱动安装、数据格式转换到建图全流程。特别针对常见编译错误和数据转换问题提供了解决方案,帮助开发者高效完成环境搭建与建图任务。

weixin_27252001的博客 113

[POJ] 2251地牢大师

来源:《信息学奥赛一本通》 , POJ 2251 算法标签 BFS 题目描述 你现在被困在一个三维地牢中,需要找到最快脱离的出路! 地牢由若干个单位立方体组成,其中部分不含岩石障碍可以直接通过,部分包含岩石障碍无法通过。 向北,向南,向东,向西,向上或向下移动一个单元距离均需要一分钟。 你不能沿对角线移动,迷宫边界都是坚硬的岩石,你不能走出边界范围。 请问,你有可能逃脱吗? 如果可以,需要多长时间...

代码中枢:软件工程理论与实践的桥梁 800

从IMU到机器人定位:手把手教你用ESKF(Error-State Kalman Filter)搞定传感器融合

本文详细介绍了如何使用ESKF(Error-State Kalman Filter)实现IMU与多传感器的融合,解决机器人定位中的积分漂移问题。通过代码示例和Gazebo仿真案例,展示了ESKF在自动驾驶和移动机器人中的核心应用,包括误差状态建模、预测过程实现和观测更新策略,帮助开发者掌握这一高效滤波技术。

weixin_33275503的博客 282

poj 2251(三维bfs)

poj 2251链接题目大意就是一个分层的迷宫,找出口,最短路径是多少。。。开一个三维数组,bfs很简单就过了,就是写起来有一点点长,也没什么特别的地方。//#include&lt;bits/stdc++.h&gt; #include&lt;cstdio&gt; #include&lt;queue&gt; #include&lt;cstring&gt; using namespace std; t...

xiange的自闭路 375

Android14_MTK调试LCD屏功能

android14 MTK平台调试LCD屏

m0_37786046的博客 1472

芒果YOLO11改进59:分类损失函数Loss篇FocalLoss:即插即用|最新改进FocalLoss损失函数,提高处理不平衡数据分类场景下的任务性能,提升YOLO11检测精度

即插即用|最新改进FocalLoss损失函数,提高处理不平衡数据分类场景下的任务性能,提升YOLO11检测精度

包括YOLOv5、YOLOv7、YOLOv8等模型改进 548

地牢大师 POJ 2251

Descriptions: 你被困在一个3D地牢中且继续寻找最短路径逃生!地牢由立方体单位构成,立方体单位中有的会充满岩石。向上下前后左右移动一个单位需要一分钟。你不能向对角线的四个方向移动且迷宫四周环绕着许多岩石。 是否可以逃出地牢?如果可以,则需要多少时间? Input 输入的第一行包含一个数,表示地牢的数量。   每个地牢的描述,其第一行包含三个数L,R和C(均小于等于30)。   L表示地...

weixin_43588422的博客 363

在isaacsim仿真系统进行遥操作(设备是quest3):怎么让遥操作设备(quest3)在Isaac sim系统中控制机械臂并把其中的参数传输出来??

我长期专注 Python 爬虫工程化实战,主理专栏 👉 《Python爬虫实战》:从采集策略到反爬对抗,从数据清洗到分布式调度,持续输出可复用的方法论与可落地案例。内容主打一个“能跑、能用、能扩展”,让数据价值真正做到——抓得到、洗得净、用得上。

**My Coding Family** 940

poj2251

 package work; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class A2251C {  static class Node {   int z;   int x;   int y;   int step;  }

emslie的博客 297

QEMU源码全解析 —— virtio(3)

QEMU源码全解析 —— virtio(3)

phmatthaus的专栏 1848

手把手教你学Simulink--人形机器人控制场景实例:基于Simulink的多连杆人形机器人拉格朗日动力学建模仿真

matlab编辑% 连杆长度 (m)% 躯干(髋到CoM)% 大腿% 小腿% 质量 (kg)% 转动惯量(绕质心,简化为杆)matlab编辑% 躯干CoM(假设在髋上方L_body/2处)% 左大腿CoM% 左小腿CoM% (右腿类似,略)本文完成了基于Simulink的多连杆人形机器人拉格朗日动力学建模仿真,实现了:✅ 定义7-DOF平面人形结构✅ 使用Symbolic Math Toolbox自动推导动力学方程✅ 生成。

MHD0815的博客 145

POJ2251 - Dungeon Master(Java代码+注释)

POJ2251 - Dungeon Master(Java代码+注释)

Mosyi_的博客 461

STM32cubemx配置驱动DHT11模块

本篇文章将带大家学习使用DHT11湿温度模块,使用这个模块可以获取当前的温度和湿度数据。DHT11可以用于智能家居,蔬菜大棚等项目当中适用范围广。DHT11 是一种数字式温湿度传感器,由杜邦公司推出。它可以测量环境中的温度和湿度,并将测量结果以数字形式传输给微控制器或其他电子设备,适用于通过数字信号读取环境温湿度的应用场景。DHT11 传感器使用单总线数据交换协议,可以通过一个 I/O 引脚进行控制和数据传输。

m0_49476241的博客 5912

poj2251 DungeonMaster BFS

poj2251 BFS

duanyuchen的博客 207

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

POJ_2251_(BFS)

B POJ 2251 三维bfs求最短路径 方向从 东南西北 加入 上下 注意L是层数的意思,从下层给到上层, bfs模板 + vis三维标记 + tx ty tz 三维坐标 #include <iostream> #include <queue> #include <string.h> using namespace std; struct node ...

_llc的博客 208

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

三维bfs Dungeon Master POJ - 2251

三维bfs Dungeon Master POJ - 2251 题目链接 一直莫名其妙的错误 。。。 两种方法 一种用到队列 另一种其实也和队列差不多 不用队列 #include&lt;stdio.h&gt; #include&lt;string.h&gt; #include&lt;iostream&gt; #include&lt;queue&gt; using namespace std; ...

qq_43559193的博客 556

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

poj-2251 Dungeon Master【bfs】

很基础的一道bfs /*这题是一个三维的迷宫题目,其中用'.'表示空地,'#'表示障碍物,'S'表示起点,'E'表示终点, 求从起点到终点的最小移动次数,解法和二维的类似, 只是在行动时除了东南西北移动外还多了上下。 对于题目给出数据的含义就是输入l,r,c,分别代表迷宫有l层,每层长宽分别是c,r。 对于数据以可以这样移动 (1,1,1)->(1,1,2)->(1,1,3)->(1,1,4)-

a915800048的专栏 1400

POJ2251—— BFS——Dungeon Master

http://poj.org/problem?id=2251 /* 简单bfs,向六个方向拓展 */ /************************************************ * Author :Powatr * Created Time :2015-8-9 9:23:15 * File Name :B.cpp ...

dgjm4087的博客 154

POJ2251 三维图BFS

POJ2251 三维图BFS 题目分析 一样的迷宫BFS,只不过换成了三维,输入的时候有些注意事项: 输入流cin无论什么情况下都会忽略tab、空格、回车等分隔符,包括但不限于cin>>int,cin>>char,cin>>string等情况。 scanf函数在除scanf("%c",&char)之外的所有情况都不会把回车符作为输入字符在输入缓存中读取...

qq_43513855的博客 140

【BFS】POJ 2251

POJ 2251 Dungeon Master 题意:有一个地图,三维,走的方向是上下,左右,前后。问你最小步数从起始点走到出口。 思路:三维的BFS,就是多加一组状态,需要细心(不细心如我就找了半个多小时的错误才AC) /** Sample Input 3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##...

weixin_33701294的博客 76

ESP32选型避坑指南:从SoC到模组,如何确定可下单料号

在嵌入式开发中,MCU选型是产品成败的关键一步。以ESP32为例,其SoC与模组在封装、射频、存储和认证上存在本质差异。SoC需自行设计外围电路和天线,而模组已集成Flash、晶振和射频匹配,并可继承无线认证,大幅降低开发门槛。对于年出货量低于10万片或缺乏射频经验的团队,模组能节省认证费用和调试时间,加快产品上市。选型时需综合协议需求、算力、Flash/PSRAM容量、天线形式及供货状态,将模糊需求转化为完整料号,并核对工具链支持。本文以智能音箱为例,演示从需求到ESP32-S3-WROOM-1-N16R

weixin_33834910的博客 323

POJ2251 Dungeon Master —— BFS

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

alince20008的博客 206

POJ 2251(WA)Dungeon Master(三维BFS)

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 unit cubes which may or may not be filled with rock. It takes one

No Rain,No L'Arc~en~Ciel 433

POJ 2251(BFS)

题目链接:http://poj.org/problem?id=2251 题意:给出一个地牢,有L层,R行C列,问在S处,能否到达E处?如果能,输出最小步数。 数据范围30,所以直接用了3维数组,模拟六种情况:上北下南左西右东天上地下。 #include #include #include #include #include #include using namespace std; con

To be excellent! 370

poj2251——bfs

POJ 2251 bfs Dungeon Master Time Limit:1000MS Memory Limit:65536K Total Submissions:18245 Accepted:7075 Description You are trapped in a 3D dungeon and need to fi...

weixin_30813225的博客 99
上一篇: POJ1426(BFS)
下一篇: POJ3414(BFS)
shui2104
博客等级 码龄10年 22粉丝 104原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值