zoukankan      html  css  js  c++  java
  • [UVA532] Dungeon Master 题解

    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 file 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!
    

    Solution

    Different to traditional maze puzzles, it's a 3-dimensional maze game.

    But actually they are the same. The only thing we have to do is turn 4-direction BFS into 6-direction one.

    Implementation

    Originlly, we have two array named dx and dy:

    const int dx[4] = {1, 0, -1, 0},
              dy[4] = {0, 1, 0, -1};
    // (dx[0], dy[0]) => right
    // (dx[1], dy[1]) => forward
    // (dx[2], dy[2]) => left
    // (dx[3], dy[3]) => backward
    

    When it comes to 3 dimensions, we have one dimension(z) and two direction(up&down) more.

    const int dz[6] = {1, 0, 0, -1, 0, 0},
              dx[6] = {0, 1, 0, 0, -1, 0},
              dy[6] = {0, 0, 1, 0, 0, -1};
    // (dx[0], dy[0], dz[0]) => up
    // (dx[1], dy[1], dz[1]) => right
    // (dx[2], dy[2], dz[2]) => forward
    // (dx[3], dy[3], dz[3]) => down
    // (dx[4], dy[4], dz[4]) => left
    // (dx[5], dy[5], dz[5]) => backward
    

    That's the core of this algorithm.

    Chore

    Input is terminated by three zeroes for L, R and C.

    In fact, most problems on UVa are like this. You have to write a while(true) loop, read inputs at the beginning of the loop, and break until you've got an invalid input.

    Code

    #include <bits/stdc++.h>
    using namespace std;
    int l, n, m;
    struct pos
    {
        int z, x, y, step;
        pos(int z, int x, int y, int step) : z(z), x(x), y(y), step(step) {}
    };
    int sz, sx, sy;
    int tz, tx, ty;
    bool mat[31][31][31];
    bool vis[31][31][31];
    const int dz[6] = {1, 0, 0, -1, 0, 0},
              dx[6] = {0, 1, 0, 0, -1, 0},
              dy[6] = {0, 0, 1, 0, 0, -1};
    bool pan(int z, int x, int y)
    {
        return z >= 1 && z <= l && x >= 1 && x <= n && y >= 1 && y <= m && mat[z][x][y] == 0;
    }
    int main()
    {
        while (true)
        {
            cin >> l >> n >> m;
            if (l == 0 && n == 0 && m == 0)
                break;
            for (int k = 1; k <= l; k++)
            {
                for (int i = 1; i <= n; i++)
                {
                    for (int j = 1; j <= m; j++)
                    {
                        char c;
                        cin >> c;
                        if (c == '.')
                            mat[k][i][j] = 0;
                        else if (c == '#')
                            mat[k][i][j] = 1;
                        else if (c == 'S')
                        {
                            mat[k][i][j] = 0;
                            sz = k;
                            sx = i;
                            sy = j;
                        }
                        else if (c == 'E')
                        {
                            mat[k][i][j] = 0;
                            tz = k;
                            tx = i;
                            ty = j;
                        }
                        else
                        {
                            cin >> c;
                        }
                        vis[k][i][j] = 0;
                    }
                }
            }
            queue<pos> q;
            q.push(pos(sz, sx, sy, 0));
            vis[sz][sx][sy] = 1;
            bool flag = 1;
            while (!q.empty())
            {
                pos head = q.front();
                q.pop();
                if (head.z == tz && head.x == tx && head.y == ty)
                {
                    cout << "Escaped in " << head.step << " minute(s).
    ";
                    flag = 0;
                    break;
                }
                for (int i = 0; i < 6; i++)
                {
                    int zz = head.z + dz[i], xx = head.x + dx[i], yy = head.y + dy[i];
                    if (!pan(zz, xx, yy))
                        continue;
                    if (vis[zz][xx][yy])
                        continue;
                    q.push(pos(zz, xx, yy, head.step + 1));
                    vis[zz][xx][yy] = 1;
                }
            }
            if (flag)
                cout << "Trapped!
    ";
        }
    }
    
  • 相关阅读:
    VS与ultraedit 正则表达式替换
    Java学习第十七天
    Java学习第十六天
    Java学习第十五天
    Java学习第十四天
    Java学习第十三天
    Java学习第十二天
    Java学习第十一天
    Java学习第十天
    Java学习第九天
  • 原文地址:https://www.cnblogs.com/water-lift/p/12912650.html
Copyright © 2011-2022 走看看