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;
        }
    }
  • 相关阅读:
    360°视频论文调研
    360°视频相机调研
    Oculus VR眼镜调研
    VR论文调研
    3ds Max学习日记(十)——显示场景资源管理器
    最近在学习Flask框架,那么就说下jinja2吧~~~
    最近学习了Sqlite3数据库,写一下操作应用以及命令
    HTML5游戏开发,剪刀石头布小游戏案例
    jquery简单的图片切换效果,支持pc端、移动端的banner图片切换开发
    jquery简单的大背景banner图片全屏切换
  • 原文地址:https://www.cnblogs.com/edych/p/7233079.html
Copyright © 2011-2022 走看看