Dungeon Master (简单BFS)

Problem 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,在普通的二维平面图上增加了层数这个维度,除了在同一层东南西北方向上行走之外,还可以在相邻层之间行走,例如:可以从(x,y,0)---->(x,y,1),读懂题意后就很好写了,BFS模板走起
#include <algorithm>
#include <bitset>
//#include <bits/extc++.h>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>

using namespace std;
//using namespace __gnu_pbds

#define ll long long
#define maxn 105

char maps[35][35][35];
int dir[6][3] = {{1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}}, step[35][35][35];
bool vis[35][35][35];
int L, R, C;

struct Node
{
    int l, r, c;
} start, end1;

bool check(Node x)
{
    if (x.l < 0 || x.l >= L || x.r < 0 || x.r >= R || x.c < 0 || x.c >= C || !vis[x.l][x.r][x.c] || maps[x.l][x.r][x.c] == ‘#‘)
    {
        return false;
    }
    return true;
}

void bfs()
{
    queue<Node> q;
    q.push(start);
    step[start.l][start.r][start.c] = 0;
    vis[start.l][start.r][start.c] = false;
    while (!q.empty())
    {
        Node now = q.front();
        q.pop();
        for (int i = 0; i < 6; ++i)
        {
            Node next = now;
            next.l += dir[i][0];
            next.r += dir[i][1];
            next.c += dir[i][2];
            // cout << next.l << "   " << next.r << "   " << next.c << endl;
            // cout << maps[next.l][next.r][next.c] << endl;
            if (check(next))
            {
                //cout << next.l << "   " << next.r << "   " << next.c << endl;
                step[next.l][next.r][next.c] = step[now.l][now.r][now.c] + 1;
                vis[next.l][next.r][next.c] = false;
                q.push(next);
            }
        }
    }
}

int main()
{
    while (scanf("%d%d%d", &L, &R, &C) && L && R && C)
    {
        memset(step, -1, sizeof(step));
        memset(vis, true, sizeof(vis));
        for (int i = 0; i < L; ++i)
        {
            for (int j = 0; j < R; ++j)
            {
                for (int k = 0; k < C; ++k)
                {
                    scanf(" %c", &maps[i][j][k]);
                    if (maps[i][j][k] == ‘S‘)
                    {
                        start.l = i;
                        start.r = j;
                        start.c = k;
                    }
                    else if (maps[i][j][k] == ‘E‘)
                    {
                        end1.l = i;
                        end1.r = j;
                        end1.c = k;
                    }
                }
            }
        }
        bfs();
        if (step[end1.l][end1.r][end1.c] != -1)
        {
            printf("Escaped in %d minute(s).\n", step[end1.l][end1.r][end1.c]);
        }
        else
        {
            puts("Trapped!");
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/whitabbit/p/12228431.html

时间: 2024-10-14 00:25:47

Dungeon Master (简单BFS)的相关文章

POJ 2251:Dungeon Master(三维BFS)

Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16178 Accepted: 6268 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 wit

POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层的地图,相同RC坐标处是相连通的.(.可走,#为墙) 解题思路:从起点开始分别往6个方向进行BFS(即入队),并记录步数,直至队为空.若一直找不到,则困住. /* POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路) */ #include <cstdio> #i

ZOJ 1940 Dungeon Master 三维BFS

Dungeon Master Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u 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 r

POJ:Dungeon Master(三维bfs模板题)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16748   Accepted: 6522 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

POJ 2251:Dungeon Master【bfs】

Dungeon Master Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submission(s) : 9   Accepted Submission(s) : 7 Problem Description You are trapped in a 3D dungeon and need to find the quickest way out! The dunge

uva 532 Dungeon Master(BFS)

uva 532 Dungeon Master 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. Y

poj 2251 Dungeon Master(三维BFS)(中等)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20598   Accepted: 7971 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

(简单) POJ 2251 Dungeon Master,BFS。

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 m

poj 2251 Dungeon Master(bfs)

题目链接  http://poj.org/problem?id=2251 题意:一个立体空间, 输入三个数,L,R,C,代表有L个平面,R行,C列,.代表可以走,#代表不能走,S代表开始点,E代表结束点,问从S开始走,对每个位置,有六个走法,即空间的六个方向的走法(上下东南西北),一分钟可以走一个点,问从S走到E点,最少可以经过多少分钟,若不能到达,则输出Trapped! 简单的三维bfs随便做一下就可以了. #include <iostream> #include <cstring&g