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;
    }
     
  • 相关阅读:
    2019年3月博客汇总
    赞美郭老师
    多项式初步
    Linux 下安装配置 JDK
    Python搜索目录下指定的文件,并返回绝对路径(包括子目录)
    Python所有的错误都是从BaseException类派生的,常见的错误类型和继承关系
    Python地址簿
    PHP正确的使用复数
    seq
    date
  • 原文地址:https://www.cnblogs.com/w-y-1/p/6729826.html
Copyright © 2011-2022 走看看