POJ 2251 Dungeon Master (三维BFS)

5 篇文章 0 订阅

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!

这就是一个三维BFS模板题,这里需要建议处理几个小细节,就是三维BFS有六个方向,如果怕被写错,debug起来很麻烦的话,建议把二维数组分开来写,也就是写成三个一维数组一一对应,这样看起来就方便许多,另外也可以把判断函数放在外面
BFS模板题有蛮多个,像简单BFS,三维BFS,双向BFS,三向BFS,这些我都会一一补齐,毕竟会了模板才会更难的题目,下面给出链接
简单BFS
双向BFS
以及这篇文章的三维BFS,三向BFS日后更新
AC代码

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
char c[35][35][35];
int vis[35][35][35];
int d[6][3] = {0, 0, 1, 0, 0, -1, 0, 1, 0, 0, -1, 0, 1, 0, 0, -1, 0, 0};
int sx, sy, sz, ex, ey, ez, n, m, k, step;
struct node
{
   int x, y, z, step;
};
int bfs()
{
   memset(vis, 0, sizeof(vis));
   queue<node> q;
   node s, e;
   s.x = sx;
   s.y = sy;
   s.z = sz;
   s.step = 0;
   vis[sx][sy][sz] = 1;
   q.push(s);
   while (!q.empty())
   {
      s = q.front();
      q.pop();
      if (s.x == ex && s.y == ey && s.z == ez)
         return s.step;
      for (int i = 0; i < 6; ++i)
      {
         e.x = s.x + d[i][0];
         e.y = s.y + d[i][1];
         e.z = s.z + d[i][2];
         if (e.x >= 1 && e.x <= n && e.y >= 1 && e.y <= m && e.z >= 1 && e.z <= k && c[e.x][e.y][e.z] != '#' && !vis[e.x][e.y][e.z])
         {
            vis[e.x][e.y][e.z] = 1;
            e.step = s.step + 1;
            q.push(e);
         }
      }
   }
   return 0;
}
int main()
{
   while (~scanf("%d%d%d", &n, &m, &k))
   {
      if (n == 0 && m == 0 && k == 0)
         break;
      for (int i = 1; i <= n; ++i)
         for (int j = 1; j <= m; ++j)
            for (int d = 1; d <= k; ++d)
            {
               scanf(" %c", &c[i][j][d]);
               if (c[i][j][d] == 'S')
               {
                  sx = i;
                  sy = j;
                  sz = d;
               }
               if (c[i][j][d] == 'E')
               {
                  ex = i;
                  ey = j;
                  ez = d;
               }
            }
      if (bfs())
         printf("Escaped in %d minute(s).\n", bfs());
      else
         printf("Trapped!\n");
   }
   return 0;
}

唯一不担心后路的方式,就是把前路走得更长些。以长远的目标看未来,才不会为眼下的不确定患得患失
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值