zoukankan      html  css  js  c++  java
  • nyoj-353 3D dungeon(搜索bfs)



    3D dungeon


    时间限制:1000 ms  |  内存限制:65535 KB 


    难度:2


    描述 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? 
    输入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.输出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!样例输入3 4 5
    S....
    .###.
    .##..
    ###.#


    #####
    #####
    ##.##
    ##...


    #####
    #####
    #.###
    ####E


    1 3 3
    S##
    #E#
    ###


    0 0 0

    样例输出

    Escaped in 11 minute(s).

    Trapped!

    #include<map>
    #include<queue>
    #include<math.h>
    #include<vector>
    #include<string>
    #include<stdio.h>
    #include<iostream>
    #include<string.h>
    #include<algorithm>
    #define inf 0x3f3f3f
    #define ll long long
    #define maxn 100005
    using namespace std;
    int l,r,c;
    char m[35][35][35];
    int n[35][35][35];
    typedef struct node
    {
        int x,y,z;
        int step;
    } t;
    t T,S;
    bool judge(int x,int y ,int z)
    {
        if(x>=0&&y>=0&&z>=0&&x<l&&y<r&&z<c)
            return true;
            return false;
    }
    int bfs()
    {
        queue<t>q;
        while(!q.empty())
        {
            q.pop();
        }
        q.push(S);
        while(!q.empty())
        {
            T=q.front();
            q.pop();
            int x=T.x;
            int y=T.y;
            int z=T.z;
            int step=T.step;
            if(m[x][y][z]=='E')
                return step;
            if(judge(x-1,y,z)&&m[x-1][y][z]!='#'&&n[x-1][y][z]==0)
            {
                n[x-1][y][z]=1;
                T.x=x-1;
                T.y=y;
                T.z=z;
                T.step=step+1;
                q.push(T);
            }
            if(judge(x,y-1,z)&&m[x][y-1][z]!='#'&&n[x][y-1][z]==0)
            {
                n[x][y-1][z]=1;
                T.x=x;
                T.y=y-1;
                T.z=z;
                T.step=step+1;
                q.push(T);
            }
            if(judge(x,y,z-1)&&m[x][y][z-1]!='#'&&n[x][y][z-1]==0)
            { 
                n[x][y][z-1]=1;
                T.x=x;
                T.y=y;
                T.z=z-1;
                T.step=step+1;
                q.push(T);
            }
            if(judge(x+1,y,z)&&m[x+1][y][z]!='#'&&n[x+1][y][z]==0)
            {
    
                n[x+1][y][z]=1;
                T.x=x+1;
                T.y=y;
                T.z=z;
                T.step=step+1;
                q.push(T);
            }
            if(judge(x,y+1,z)&&m[x][y+1][z]!='#'&&n[x][y+1][z]==0)
            {
                n[x][y+1][z]=1;
                T.x=x;
                T.y=y+1;
                T.z=z;
                T.step=step+1;
                q.push(T);
            }
            if(judge(x,y,z+1)&&m[x][y][z+1]!='#'&&n[x][y][z+1]==0)
            {
                n[x][y][z+1]=1;
                T.x=x;
                T.y=y;
                T.z=z+1;
                T.step=step+1;
                q.push(T);
            }
        }
        return -1;
    }
    int main()
    {
        while(~scanf("%d%d%d",&l,&r,&c))
        {
            memset(m,0,sizeof(m));
            for(int i=0; i<l; i++)
                for(int j=0; j<r; j++)
                {
                    cin>>m[i][j];
                    for(int k=0; k<c; k++){
                        if(m[i][j][k]=='S')
                        {
                            S.x=i;
                            S.y=j;
                            S.z=k;
                            S.step=0;
                        }
                        n[i][j][k]=0;
                    }
                }
            int ans=bfs();
            if(ans!=-1)
                printf("Escaped in %d minute(s).
    ",ans);
            else
                printf("Trapped!
    ");
        }
    }




  • 相关阅读:
    code第一部分数组:第十七题 爬楼梯问题
    code第一部分数组:第十六题 数组表示数,加一操作
    code第一部分数组:第十五题 矩阵翻转(图像翻转)
    code第一部分:数组 第十四题 雨水问题
    code第一部分数组:第十题:移除数组中目标元素
    [java 2020-04-24] Maven多模块项目+SpringBoot,编译失败:程序包xxx不存在
    [java 2020-04-24] springboot生成的jar包没有主类和依赖jar包
    [java 2019-12-16] SimpleDateFormat格式化跨年显示格式错误
    [2019-11-22]马说
    [linux 2019-10-23] linux可以创建多少个用户
  • 原文地址:https://www.cnblogs.com/da-mei/p/9053282.html
Copyright © 2011-2022 走看看