zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    Unity5 GI与PBS渲染从用法到着色代码
    Unity场景渲染相关实现的猜想
    Ogre2.1 Hlms与渲染流程
    Ogre2.1 灯光与阴影
    Ogre2.1 结合OpenGL3+高效渲染
    Ogre2.0 全新功能打造新3D引擎
    Ogre 编辑器三(自动生成与更新Ogre对象编辑界面)
    Ogre 编辑器二(用Ogre的地形组件加载天龙八部地形)
    一个简单的旋转控制器与固定屏幕位置
    sql 触发器里,发生错误,回滚提示错误语句
  • 原文地址:https://www.cnblogs.com/CuteAbacus/p/9492109.html
Copyright © 2011-2022 走看看