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;
    }

     

     

     

  • 相关阅读:
    c# Array或List有个很实用的ForEach方法,可以直接传入一个方法对集合中元素操作
    js 检查字符串中是否包含中文(正则)
    Js 数组对象排序
    Js日期处理
    JS 检测字符串是否还有某个字符
    js获取URL传参
    使用JavaScript修改浏览器URL地址栏的实现代码
    上传、裁剪图片-----Jcrop图片裁剪插件
    zip拉链方法
    内置函数如help()...
  • 原文地址:https://www.cnblogs.com/yinbiao/p/9501772.html
Copyright © 2011-2022 走看看