• POJ 2251 Dungeon Master


    Dungeon Master
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 28381   Accepted: 11090

    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!

    知道怎么做,单总是细节方面出错,看了一下他们的代码,感觉收获很多。
    参考来源:http://blog.csdn.net/weizhuwyzc000/article/details/48933891
    下面是自己写的代码:
    #include <iostream>
    #include <queue>
    using namespace std;
    
    const int N = 35;
    
    struct Node
    {
        int x, y, z;
        Node(int x=0, int y=0, int z=0) :x(x), y(y), z(z){}
        bool operator ==(const Node &rsh)const {
            return rsh.x == x&&rsh.y == y&&rsh.z == z;
        }
    }S, E;
    
    int nxt[6][3]{
        0,0,-1,
        0,0,1,
        1,0,0,
        -1,0,0,
        0,1,0,
        0,-1,0
    };
    
    char map[N][N][N];
    int book[N][N][N];
    int L, C, R;
    
    int bfs() {
        memset(book, -1, sizeof(book));
        book[S.z][S.x][S.y] = 0;
        queue<Node>q;
        q.push(S);
    
        while (!q.empty()) {
            Node u = q.front();
            q.pop();
            if (u == E)
                return book[u.z][u.x][u.y];
    
            for (int i = 0; i < 6; i++) {
                int x = u.x + nxt[i][0], y = u.y + nxt[i][1], z = u.z + nxt[i][2];
    
                if (x<1 || x>R || y<1 || y>C || z<1 || z>L) continue;
                if (map[z][x][y] == '.'&&book[z][x][y] == -1) {
                    book[z][x][y] = book[u.z][u.x][u.y]+1;
                    q.push(Node(x, y, z));
                }
    
            }
    
        }
        return -1;
    }
    
    
    int main() {
        while (cin >> L >> R>> C) {
            cin.get();
            for (int i = 1; i <= L; i++)
                for (int j = 1; j <= R; j++)
                    cin >> (map[i][j] + 1);
    
            for (int i = 1; i <= L; i++)
                for (int j = 1; j <= R; j++)
                    for (int k = 1; k <= C; k++)
                        if (map[i][j][k] == 'S')
                            S = Node(j, k, i), map[i][j][k] = '.';
                        else if (map[i][j][k] == 'E')
                            E = Node(j, k, i), map[i][j][k] = '.';
            int ans = bfs();
            if (ans == -1) printf("Trapped!
    ");
            else printf("Escaped in %d minute(s).
    ", ans);
        }
    }
    自己选的路,跪着也要把它走完------ACM坑
  • 相关阅读:
    Redis到底该如何利用?
    AutoMapper搬运工之自定义类型转换
    AutoMapper搬运工之初探AutoMapper
    【ELK】docker-compose搭建ELK单机环境
    [flowable工作流引擎]基本概念及术语
    shell遍历文件夹读取文件夹下的文件
    vector类的简单实现
    string类的实现
    接口安全认证
    C# lock private VS private static
  • 原文地址:https://www.cnblogs.com/IKnowYou0/p/6104078.html
Copyright © 2020-2023  润新知