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
  • 相关阅读:
    Maven CXF wsdl2Java String生成JAXBElement<Xxx> 解决方法
    Maven私服(Nexus)资源上传下载
    Maven私服(Nexus)启动创建Windows服务
    第三方jar上传到Maven私服(Nexus)
    Maven下CXF的wsdl2java生成客户端代码
    [解决]CXF wsdl2java 生成代码存在的一些问题
    Link Shell Extension
    Weblogic环境(JSP)文件下载问题(下载的文件与原文件大小不一致问题)
    腾讯QQ形象18年变迁史,最早的QQ企鹅形象居然长这样!
    为何称她为“互联网女皇”?知道真相后不得不佩服!
  • 原文地址:https://www.cnblogs.com/clliff/p/4241035.html
Copyright © 2011-2022 走看看