• Dungeon Master POJ 2251


    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!
    #include <iostream>
    #include <utility>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <cstdio>
    #include <cmath>
    #include <string>
    #include <sstream>
    #include <functional>
    using namespace std;
    
    struct Node {
        int x, y, z;
        int steps;
    };
    struct Node Start, End;
    int l, r, c;
    bool vis[32][32][32];
    char mmap[32][32][32];
    
    int bfs() {
        int next[6][3] = { { -1,0,0 },{ 0,-1,0 },{ 0,0,-1 },{ 1,0,0 },{ 0,1,0 },{ 0,0,1 } };
        queue<Node> q;
        q.push(Start);
        vis[Start.x][Start.y][Start.z] = true;
        while (!q.empty()) {
            struct Node temp = q.front();
            q.pop();
            if (mmap[temp.x][temp.y][temp.z] == 'E')return temp.steps;
            for (int i = 0; i < 6; i++) {
                struct Node add;
                add.x = temp.x + next[i][0];
                add.y = temp.y + next[i][1];
                add.z = temp.z + next[i][2];
                add.steps = temp.steps + 1;
                if (add.x >= 0 && add.y >= 0 && add.z >= 0 && add.x < r&&add.y < c&&add.z < l
                    &&mmap[add.x][add.y][add.z] != '#'&&!vis[add.x][add.y][add.z])//此处特别注意!=‘#’而不是==‘.'这样的话E就被排除在外了
                {
                    vis[add.x][add.y][add.z] = true;
                    q.push(add);
                }
            }
        }
        return -1;
    }
    int main() {
        while (scanf("%d %d %d", &l, &r, &c) != EOF)
        {
            memset(vis, false, sizeof(vis));
            if (l == 0 && r == 0 && c == 0)
                break;
            for (int i = 0; i<l; i++)
            {
                getchar();
                for (int j = 0; j<r; j++)
                {
                    for (int k = 0; k<c; k++)
                    {
                        scanf("%c", &mmap[j][k][i]);
                        if (mmap[j][k][i] == 'S')
                        {
                            Start.x = j; Start.y = k; Start.z = i;
                        }
                    }
                    getchar();
                }
            }
            int result = bfs();
            if (result != -1)
                printf("Escaped in %d minute(s).\n", result);
            else
                printf("Trapped!\n");
        }
        return 0;
    }
  • 相关阅读:
    LeetCode 167. 两数之和 II
    LeetCode 97. 交错字符串
    LeetCode 35. 搜索插入位置
    LeetCode 120. 三角形最小路径和
    LeetCode 350. 两个数组的交集 II
    LeetCode 174. 地下城游戏
    LeetCode 315. 计算右侧小于当前元素的个数
    LeetCode 309. 最佳买卖股票时机含冷冻期
    面试题 17.13. 恢复空格
    去除字符串首尾空格
  • 原文地址:https://www.cnblogs.com/CuteAbacus/p/9492109.html
Copyright © 2020-2023  润新知