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;
    }
  • 相关阅读:
    java类型转换
    JVM内存各个区域分工简单介绍
    用数组实现栈
    一些关于Spring的随笔
    设计模式学习笔记(三)之静(动)态代理模式、适配器模式
    浅谈经典排序算法
    PetStore项目总结
    设计模式学习笔记(二)之观察者模式、装饰者模式
    Spring的校验(Validator)
    设计模式学习笔记(一)之工厂模式、单例模式
  • 原文地址:https://www.cnblogs.com/CuteAbacus/p/9492109.html
Copyright © 2011-2022 走看看