• 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!
    ");
        }
    }




  • 相关阅读:
    Qt调用外部程序QProcess通信
    QT错误:collect2:ld returned 1 exit status
    ARM编译空间属性(转)
    深入C语言内存区域分配(进程的各个段)详解(转)
    Linux系统的组成和内核的组成
    C语言中,头文件和源文件的关系(转)
    Ubuntu安装samba服务器
    2018年应该做的事
    生活经历1
    学习笔记
  • 原文地址:https://www.cnblogs.com/da-mei/p/9053282.html
Copyright © 2020-2023  润新知