• 【搜索】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;  
    }  
    
  • 相关阅读:
    RubyConf的podcast
    一篇很好的英语学习文章:一个孤独而封闭世界――英语口语
    新浪和搜狐的读书频道
    新想法:个性化的RSS
    代码搜索:Koders
    我看到的Web 2.0: 自组织的大众化参与
    土豆网的后舍男孩挺搞笑的
    可以给pdf加批注的软件VeryPDF PDF Editor
    张海迪写的描写英语学习经验的书《美丽的英语》
    Fowler出来推荐Rake了(基于Ruby的build工具)
  • 原文地址:https://www.cnblogs.com/KID-XiaoYuan/p/6350319.html
Copyright © 2020-2023  润新知