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;  
    }  
    
  • 相关阅读:
    在谷歌地图上绘制行政区域轮廓【结合高德地图的API】
    用PL/SQL远程连接Oracle服务器
    找工作之离职篇
    linux设置定时备份mysql数据库
    使用NoSQL实现高并发CRM系统实践(源代码+解析)
    做领导应该注意的几个问题
    如何才能成为真正的程序员
    利用websocket实现手机扫码登陆后,同步登陆信息到web端页面
    利用laravel-echo主动向服务端发送消息,实现在线状态管理
    飞鱼CRM
  • 原文地址:https://www.cnblogs.com/KID-XiaoYuan/p/6350319.html
Copyright © 2011-2022 走看看