zoukankan      html  css  js  c++  java
  • POJ 2251 Dungeon Master(多层地图找最短路 经典bfs,6个方向)

    Dungeon Master
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 48380   Accepted: 18252

    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!
    

    Source

     

    跟平常的bfs问题的区别就是方向有6个

    #include<cstdio>
    #include<string>
    #include<cstdlib>
    #include<cmath>
    #include<iostream>
    #include<cstring>
    #include<set>
    #include<queue>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<cctype>
    #include<stack>
    #include<sstream>
    #include<list>
    #include<assert.h>
    #include<bitset>
    #include<numeric>
    #define max_v 35
    using namespace std;
    int dir[6][3]={{0,0,1},{0,1,0},{0,0,-1},{0,-1,0},{1,0,0},{-1,0,0}};//6个方向 东,南,西,北,上,下
    char G[max_v][max_v][max_v];
    int vis[max_v][max_v][max_v];
    int sx,sy,sz,fx,fy,fz;
    int n,m,k;
    int ans;
    struct node
    {
        int x,y,z;
        int step;
    };
    bool check(node a)//检查该点合法性
    {
        if(a.x<0||a.x>=n||a.y<0||a.y>=m||a.z<0||a.z>=k)
            return false;
        else if(G[a.z][a.x][a.y]=='#')
            return false;
        else if(vis[a.z][a.x][a.y])
            return false;
        else
            return true;
    }
    void bfs(int x,int y,int z)
    {
        queue<node> q;
        node p,next;
    
        p.x=x;
        p.y=y;
        p.z=z;
        p.step=0;
    
        q.push(p);
    
        while(!q.empty())
        {
            p=q.front();
            q.pop();
    
            if(p.x==fx&&p.y==fy&&p.z==fz)
            {
                ans=p.step;
                return ;
            }
    
            for(int i=0;i<6;i++)
            {
                next.x=p.x+dir[i][2];
                next.y=p.y+dir[i][1];
                next.z=p.z+dir[i][0];
    
                if(check(next))
                {
                    vis[next.z][next.x][next.y]=1;
                    next.step=p.step+1;
                    q.push(next);
                }
    
            }
        }
    }
    int main()
    {
        while(~scanf("%d %d %d",&k,&n,&m))
        {
            if(n==0&&m==0&&k==0)
                break;
    
            for(int z=0;z<k;z++)
            {
                for(int i=0;i<n;i++)
                {
                    scanf("%s",G[z][i]);
                    for(int j=0;j<m;j++)
                    {
                        if(G[z][i][j]=='S')
                            sx=i,sy=j,sz=z;
                        else if(G[z][i][j]=='E')
                            fx=i,fy=j,fz=z;
                    }
                }
            }
    
            memset(vis,0,sizeof(vis));
            ans=-1;
    
            bfs(sx,sy,sz);
    
            if(ans==-1)
                printf("Trapped!
    ");
            else
                printf("Escaped in %d minute(s).
    ",ans);
        }
        return 0;
    }

     

     

     

  • 相关阅读:
    ubuntu20.04安装教程TLS,ubuntu安装教程20.04
    ubuntu20.04安装教程,ubuntu详细安装教程20.04
    ubuntu20.04安装教程,ubuntu20.04图文安装教程
    ubuntu20.04安装教程,ubuntu详细安装教程20.04
    ubuntu20.04安装教程,ubuntu安装教程20.04
    ubuntu19.10安装搜狗输入法,ubuntu19.10安装中文输入法
    ubuntu20.04安装搜狗输入法,ubunru安装搜狗输入法中文输入法拼音
    前端实现实时通讯
    移动端1px边框的实现
    利用js自动触发一个a标签的下载事件
  • 原文地址:https://www.cnblogs.com/yinbiao/p/9501772.html
Copyright © 2011-2022 走看看