zoukankan      html  css  js  c++  java
  • POJ-2251-Dungeon Master

    http://poj.org/problem?id=2251

                                            Dungeon Master
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 32885   Accepted: 12601

    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<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<math.h>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #include<map>
    
    using namespace std;
    int dir[6][3]= {{1, 0, 0}, {-1, 0, 0}, {0, 0, -1}, {0, 0, 1}, {0, -1, 0}, {0, 1, 0}};
    struct node
    {
        int x, y, z, step;
    };
    char maps[50][50][50];
    int vis[50][50][50];
    int l, r, c, sx, sy, sz;
    
    int bfs()
    {
        queue<node>Q;
        node p;
        memset(vis, 0, sizeof(vis));
        p.x=sx;
        p.y=sy;
        p.z=sz;
        p.step=0;
        vis[sx][sy][sz]=1;
        Q.push(p);
        while(!Q.empty())
        {
            node q, next;
            q=Q.front();
            Q.pop();
            for(int i=0; i<6; i++)
            {
                next.x=q.x+dir[i][0];
                next.y=q.y+dir[i][1];
                next.z=q.z+dir[i][2];
                if(next.x>=0&&next.x<l&&next.y>=0&&next.y<r&&next.z>=0&&next.z<c&&!vis[next.x][next.y][next.z]&&(maps[next.x][next.y][next.z]=='.'||maps[next.x][next.y][next.z]=='E'))
                {
                    vis[next.x][next.y][next.z]=1;
                    next.step=q.step+1;
                    if(maps[next.x][next.y][next.z]=='E')
                    {
                        return next.step;
                    }
                    Q.push(next);
                }
            }
        }
        return 0;
    }
    int main()
    {
        while(scanf("%d %d %d", &l, &r, &c), l+r+c)
        {
            getchar();
            for(int i=0; i<l; i++,getchar())
            {
    
                for(int j=0; j<r; j++,getchar())
                {
                    for(int k=0; k<c; k++)
                    {
                        scanf("%c", &maps[i][j][k]);
                        if(maps[i][j][k]=='S')
                        {
                            sx=i, sy=j, sz=k;
                        }
                    }
                }
            }
            int ans=bfs();
            if(ans)
                printf("Escaped in %d minute(s).
    ", ans);
            else
                printf("Trapped!
    ");
        }
        return 0;
    }
     
  • 相关阅读:
    html-标题标签、水平线标签和特殊字符
    htm-文字标签和注释标签
    html-html简介
    scss-函数
    scss-!optional
    scss-@extend
    解决SecureCRT下spark-shell中scala无法删除问题
    Python list降序排序
    Python 字典的一键多值,即一个键对应多个值
    python 数组中如何根据值,获取索引,如何根据索引删除值 , 以及如何根据值删除值
  • 原文地址:https://www.cnblogs.com/w-y-1/p/6729826.html
Copyright © 2011-2022 走看看