zoukankan      html  css  js  c++  java
  • Dungeon Master(三维bfs)

    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<cstdio>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<set>
    #include<map>
    #include<vector>
    #include<cmath>
    
    const int maxn=1e5+5;
    typedef long long ll;
    char Map[35][35][35];
    int vis[35][35][35];
    struct node
    {
        int x,y,z;
        int cnt;
    };
    using namespace std;
    int sx,sy,sz;
    int ex,ey,ez;
    int dir[6][3]={{0,0,1},{0,0,-1},{-1,0,0},{1,0,0},{0,1,0},{0,-1,0}};
    int s;
    bool check(int x,int y,int z)
    {
        if((Map[z][x][y]=='.'||Map[z][x][y]=='E')&&vis[z][x][y]==0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    bool bfs()
    {
        node start;
        start.x=sx;
        start.y=sy;
        start.z=sz;
        start.cnt=0;
        queue<node>q;
        q.push(start);
        vis[sz][sx][sy]=1;
        while(!q.empty())
        {
            node now=q.front();
            q.pop();
            //printf("%d %d %d
    ",now.z,now.x,now.y);
            if(now.z==ez&&now.x==ex&&now.y==ey)
            {
                 s=now.cnt;
                 return true;
            }
            for(int t=0;t<6;t++)
            {
                node next;
                next.x=now.x+dir[t][0];
                next.y=now.y+dir[t][1];
                next.z=now.z+dir[t][2];
                next.cnt=now.cnt+1;
                if(check(next.x,next.y,next.z))
                {
                    vis[next.z][next.x][next.y]=1;
                    q.push(next);
                }
                
            }
        }
        return false;
    }
    int main()
    {
        int L,R,C;
        while(scanf("%d%d%d",&L,&R,&C)!=EOF)
        {
            memset(vis,0,sizeof(vis));
            if(L==0&&R==0&&C==0)
            {
                break;
            }
            s=0;
            for(int t=0;t<L;t++)
            {
                for(int j=0;j<R;j++)
                {
                  scanf("%s",Map[t][j]);    
                }
            }
            for(int t=0;t<L;t++)
            {
                for(int j=0;j<R;j++)
                {
                    for(int k=0;k<C;k++)
                    {
                        if(Map[t][j][k]=='S')
                        {
                            sx=j;
                            sy=k;
                            sz=t;
                        }
                        if(Map[t][j][k]=='E')
                        {
                            ex=j;
                            ey=k;
                            ez=t;
                        }
                    }
                }
            }
            
            if(bfs())
            printf("Escaped in %d minute(s).
    ",s);
            else
            {
                printf("Trapped!
    ");
            }
        }
        
        return 0;
    }
  • 相关阅读:
    Quartz.net 定时任务在IIS中未按时执行
    扩展方法
    mysql 实用语句
    jquery each map
    js匿名函数多时注意
    ASP.NET MVC3调用分部视图
    eclipse快捷键
    regular 点滴
    适配器模式
    php代码实现简单图片下载
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10823712.html
Copyright © 2011-2022 走看看