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
  • 相关阅读:
    60.django session缓存配置
    Jmeter学习笔记-Jmeter目录文件
    Jmeter用户手册目录
    Jmeter学习笔记-jmeter执行结束报错:The JVM should have exitted but did not
    PPT-分割图片
    bug记录-权限问题,比如说默认搜索条件为子系统,则在该子系统下面的接口模版,在搜索条件下也应该有权限的限制
    bug记录-弹出框,刚配置的时候记录少,当记录过多时,容易出现高度过高,所以要对高度进行限制,以及设置滚动条
    Jmeter学习笔记-20181224安装软件路径及linux安装方法
    bug记录-列表头部缺失,点开页面,列表头部存在,然后点击搜索后,某个枚举没有值,搜出来没有列表头部,换成其他有值的列表头部,列表头部不显示了
    视频录制与剪辑备忘记录
  • 原文地址:https://www.cnblogs.com/clliff/p/4241035.html
Copyright © 2011-2022 走看看