zoukankan      html  css  js  c++  java
  • 【搜索】Dungeon Master

    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!
    

    #include<iostream>
    #include <cstdio>  
    #include <cstring>  
    #include <queue>  
    using namespace std;  
    const int maxn = 35;  
      
    int sx,sy,sz,ex,ey,ez,x,y,z;    
    char cube[maxn][maxn][maxn];  
    int vis[maxn][maxn][maxn];  
    int dir[6][3] = {1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,1,0,0,-1};  
      
    struct Node  
    {  
        int x,y,z,t;  
        Node(int i,int j,int m,int n):x(i),y(j),z(m),t(n){} //构造  
        Node(){}  
    }pre;  
      
    bool judge(int i, int j, int k)   //边界  
    {  
        if(i < 0 || i >= x ||j < 0 || j >= y || k < 0 || k >= z)  
            return false;  
        return true;  
    }  
      
    void bfs()  
    {  
        memset(vis,0,sizeof(vis));  
        queue <Node> que;  
        que.push(Node(sx,sy,sz,0));  
        vis[sx][sy][sz] = 1;  
        while(!que.empty())  
        {  
            pre = que.front(); que.pop();  
            if(pre.x == ex && pre.y == ey && pre.z == ez)  
            {  
                printf("Escaped in %d minute(s).
    ", pre.t);  
                return ;  
            }  
            for(int i = 0; i < 6; i++)  
            {  
                int xx = pre.x + dir[i][0];  
                int yy = pre.y + dir[i][1];  
                int zz = pre.z + dir[i][2];  
                if(!vis[xx][yy][zz] && judge(xx,yy,zz) && cube[xx][yy][zz] != '#')  
                {  
                    vis[xx][yy][zz] = 1;  
                    que.push(Node(xx,yy,zz,pre.t+1));  
                }  
            }  
        }  
        printf("Trapped!
    ");  
    }  
      
    int main()  
    {  
        while(scanf("%d %d %d", &x, &y, &z) != EOF)  
        {  
            if(!x && !y && !z) break;  
            for(int i = 0; i < x; i++)  
                for(int j = 0; j < y; j++)  
                    scanf("%s", cube[i][j]);  
            for(int i = 0; i < x; i++)  
                for(int j = 0; j < y; j++)  
                    for(int k = 0; k < z; k++)  
                        if(cube[i][j][k] == 'S')  
                            sx = i,sy = j,sz = k;  
                        else if(cube[i][j][k] == 'E')  
                            ex = i,ey = j,ez = k;  
            bfs();  
        }  
        return 0;  
    }  
    
  • 相关阅读:
    ThinkPHP 5 中AJAX跨域请求头设置方法【转】
    phpstorm激活大全【转】
    PHP严重致命错误处理:php Fatal error: Cannot redeclare class or function【转】
    php的dirname(__FILE__)和dirname(dirname(__FILE__))使用总结【转】
    php 设置临时内存和超时设置脚本最大执行时间
    mysql 实现表连接(左,右,内,全连接)【转】
    Maximum execution time of 30 seconds exceeded解决办法【转】
    常用邮箱SMTP服务器地址大全
    叉积(POJ
    乘法逆元(P3811)(四种方法)
  • 原文地址:https://www.cnblogs.com/KID-XiaoYuan/p/6350319.html
Copyright © 2011-2022 走看看