zoukankan      html  css  js  c++  java
  • Dungeon Master (BFS)

    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<cstdio>
    #include<cstring>
    #include<string>
    #include<set>
    #include<vector>
    #include<stack>
    #include<queue>
    #include<algorithm>
    #include<cstdio>
    #include<algorithm>
    #include<functional>
    #include<sstream>
    #define N 50  
    using namespace std;
    char map[N][N][N] = { 0 };
    bool vis[N][N][N] = { 0 };
    int dis[N][N][N] = { 0 };
    int move[6][3] = { { 0,0,1 },{ 0,0,-1 },{ 0,1,0 },{ 0,-1,0 },{ 1,0,0 },{ -1,0,0 } };
    int x, y, z;
    struct point
    {
        int x; int y; int z;
    };
    point e, s;
    void input()
    {
        char l;
        l = getchar();
        for (int i = 1; i <= x; i++)
        {
            for (int j = 1; j <= y; j++)
            {
                for (int h = 1; h <= z; h++)
                {
                    scanf("%c", &map[i][j][h]);
                    if (map[i][j][h] == 'S')
                    {
                        s.x = i;
                        s.y = j;
                        s.z = h;
                    }
                    if (map[i][j][h] == 'E')
                    {
                        e.x = i;
                        e.y = j;
                        e.z = h;
                    }
                }
                l = getchar();
            }
            l = getchar();
        }
    
    }
    bool is_inmap(point t)
    {
        if (t.x <= 0 || t.x>x)return 0;
        if (t.y <= 0 || t.y>y)return 0;
        if (t.z <= 0 || t.z>z)return 0;
        return 1;
    }
    void bfs()
    {
        queue<point>poi;
        poi.push(s);
        vis[s.x][s.y][s.z] = 1;
        while (!poi.empty())
        {
            for (int i = 0; i<6; i++)
            {
                point t;
                t.x = poi.front().x + move[i][0];
                t.y = poi.front().y + move[i][1];
                t.z = poi.front().z + move[i][2];
                if (vis[t.x][t.y][t.z] == 1)continue;
                if (!is_inmap(t))continue;
                if (map[t.x][t.y][t.z] == '#')continue;
    
                dis[t.x][t.y][t.z] = dis[poi.front().x][poi.front().y][poi.front().z] + 1;
                vis[t.x][t.y][t.z] = 1;
                poi.push(t);
            }
            poi.pop();
        }
    }
    
    int main()
    {
        while (cin >> x >> y >> z)
        {
            if (x == 0 && y == 0 && z == 0)break;
            memset(dis, 0, sizeof(dis));
            memset(vis, 0, sizeof(vis));
            input();
            bfs();
            if (dis[e.x][e.y][e.z] == 0)cout << "Trapped!" << endl;
            else cout << "Escaped in " << dis[e.x][e.y][e.z] << " minute(s)." << endl;
        }
    }
  • 相关阅读:
    java如何将char类型的数字转换成int型的数字,而不是Ascii
    java 二分查找的注意事项
    IntelliJ IDEA 下的svn配置及使用的非常详细的图文总结
    java中Math的常用方法整理
    判断字符串是否可由重复子字符串组成
    P3558 [POI2013]BAJ-Bytecomputer
    BZOJ 3329. Xorequ
    Codeforces 1221F. Choose a Square
    Codeforces 1221E. Game With String
    Codeforces 1221D. Make The Fence Great Again
  • 原文地址:https://www.cnblogs.com/edych/p/7233079.html
Copyright © 2011-2022 走看看