zoukankan      html  css  js  c++  java
  • POJ 2251 Dungeon Master(BFS)

    Dungeon Master
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 17465   Accepted: 6792

    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

     
    搜索水题,但是要注意不仅有往下层走的情况还有往上层走的情况。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<stdlib.h>
    #include<queue>
    #include<stack>
    #include<algorithm>
    #define LL __int64
    using namespace std;
    const int MAXN=30+5;
    const int INF=0x3f3f3f3f;
    const double EPS=1e-9;
    int dir4[][2]={{0,1},{1,0},{0,-1},{-1,0}};
    int dir8[][2]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};
    int dir_8[][2]={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};
    int f,n,m,vis[MAXN][MAXN][MAXN],ans;
    char map[MAXN][MAXN][MAXN];
    struct node
    {
        int z,x,y;
        int step;
        bool operator<(const node A)const
        {
            return step>A.step;
        }
    }star,en;
    
    bool judge(int x,int y,int z)
    {
        if(0<=x && x<n && 0<=y && y<m && 0<=z && z<f && !vis[z][x][y] && map[z][x][y]!='#')
            return true;
        return false;
    }
    int BFS()
    {
        memset(vis,0,sizeof(vis));
        priority_queue<node> Q;
        Q.push(star);
        vis[star.z][star.x][star.y]=1;
        while(!Q.empty())
        {
            node now,next;
            now=Q.top();
            Q.pop();
            //printf("(%d %d %d) %d
    ",now.z,now.x,now.y,now.step);
            if(now.z==en.z && now.x==en.x && now.y==en.y)
                return now.step;
    
            for(int i=0;i<4;i++)
            {
                next.x=now.x+dir4[i][0];
                next.y=now.y+dir4[i][1];
                next.z=now.z;
                if(judge(next.x,next.y,next.z))
                {
                    vis[next.z][next.x][next.y]=1;
                    next.step=now.step+1;
                    Q.push(next);
                }
            }
            next.z=now.z+1;
            next.x=now.x;
            next.y=now.y;
            if(judge(next.x,next.y,next.z))
            {
                vis[next.z][next.x][next.y]=1;
                next.step=now.step+1;
                Q.push(next);
            }
    
            next.z=now.z-1;
            next.x=now.x;
            next.y=now.y;
            if(judge(next.x,next.y,next.z))
            {
                vis[next.z][next.x][next.y]=1;
                next.step=now.step+1;
                Q.push(next);
            }
    
        }
    
        return INF;
    }
    int main()
    {
        while(scanf("%d %d %d",&f,&n,&m) && (f||n||m))
        {
            memset(map,0,sizeof(map));
            for(int i=0;i<f;i++)
                for(int j=0;j<n;j++)
                 {
                     cin>>map[i][j];
                     for(int k=0;k<m;k++)
                     {
                         if(map[i][j][k]=='S')
                         {
                             star.z=i;
                             star.x=j;
                             star.y=k;
                             star.step=0;
                         }
                         if(map[i][j][k]=='E')
                         {
                             en.z=i;
                             en.x=j;
                             en.y=k;
                         }
                     }
                 }
    
            ans=BFS();
            if(ans==INF) printf("Trapped!
    ");
            else printf("Escaped in %d minute(s).
    ",ans);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    前端展示(四)
    小谢第66问:页面关闭鼠标光标
    小谢第64问:nuxt项目中增加百度分析统计
    js 判断当前是手机还是电脑
    布谷鸟自定义教程
    vs code常用插件及配置
    小程序几件小事儿
    删除 json 数据中的某一项
    小程序图片预览
    小程序 navigator 取消点击效果
  • 原文地址:https://www.cnblogs.com/clliff/p/4241035.html
Copyright © 2011-2022 走看看