• POJ Problem 2251 Dungeon Master 【BFS】


    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 26407   Accepted: 10275

    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!
    

    Source

     三维的广搜,只是比二维多几个方向而已。

    #include <cstdio>
    #include <queue>
    #include <cstring>
    using namespace std;
    
    int k, m, n;
    struct node {
        int x, y, z, step;
    }temp, p, X, Y;
    bool vis[32][32][32];
    char map[32][32][32];
    int dx[6] = {0,0,1,0,0,-1};
    int dy[6] = {0,1,0,0,-1,0};
    int dz[6] = {1,0,0,-1,0,0};
    bool judge(node a) {
        if(a.x<0 || a.y<0 || a.z<0 || a.x>=k || a.y>=n || a.z>=m)
            return true;
        else if(map[a.x][a.y][a.z] == '#')
            return true;
        else if(vis[a.x][a.y][a.z])
            return true;
        return false;
    }
    int bfs(node a, node b) {
        memset(vis,false,sizeof(vis));
        queue<node> q;
        while (!q.empty())   q.pop();
        a.step = 0;
        q.push(a);
        vis[a.x][a.y][a.z] = true;
        while (q.size()) {
            p = q.front();
            q.pop();
            if(p.x == b.x&&p.y == b.y&&p.z == b.z)
                return p.step;
            for(int i = 0; i < 6; i++) {
                temp.x = p.x + dx[i];
                temp.y = p.y + dy[i];
                temp.z = p.z + dz[i];
                if (judge(temp)) continue;
                vis[temp.x][temp.y][temp.z] = true;
                temp.step = p.step + 1;
                q.push(temp);
            }
        }
        return -1;
    }
    
    int main() {
        while (scanf("%d%d%d", &k, &n, &m) != EOF) {
            if (!k && !n && !m) break;
            for (int i = 0; i < k; i++) {
                for (int j = 0; j < n; j++) {
                    scanf("%s",map[i][j]);
                    for(int l = 0; l < m; l++) {
                        if(map[i][j][l] == 'S')
                            X.x = i, X.y = j, X.z = l;
                        else if(map[i][j][l] == 'E')
                            Y.x = i, Y.y = j, Y.z = l;
                    }
                }
            }
            int ans = bfs(X, Y);
            if (ans == -1) printf("Trapped!
    ");
            else printf("Escaped in %d minute(s).
    ", ans);
        }
    }
    
    

     

  • 相关阅读:
    爬取药智网中的方剂信息
    日报3.13
    数据库添加出错
    Bencode
    一些安全网络协议
    代码质量不重要
    Jordan Peterson
    随身记录的缺点
    Why is Go PANICking?
    go问
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770891.html
Copyright © 2020-2023  润新知