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

    Dungeon Master
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 14268   Accepted: 5552

    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
     
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <queue>
    using namespace std;
    int map[31][31][31];
    int hash[31][31][31];
    int move[6][3] = {1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,1,0,0,-1};
    int l,r,c;
    int flag;
    int sx,sy,sz,ex,ey,ez;
    struct Node
    {
        int x,y,z;
        int time;
    };
    void BFS()
    {
        flag = 0;
        queue <Node> q;
        Node top;top.x = sx;top.y = sy;top.z = sz;top.time = 0;
        q.push(top);
        while(!q.empty())
        {
            Node temp = q.front();q.pop();
            if(temp.x == ex && temp.y == ey && temp.z == ez)
            {
                flag = 1;
                printf("Escaped in %d minute(s). ",temp.time);
                return ;
            }
            for(int i = 0;i < 6;i ++)
            {
                int x = temp.x + move[i][0];int y = temp.y + move[i][1];
                int z = temp.z + move[i][2];int time = temp.time + 1;
                if(!hash[x][y][z] && map[x][y][z] != '#' && x >= 1 && x <= l && y >= 1 && y <= r && z >= 1 && z <= c)
                {
                    hash[x][y][z] = 1;Node step;
                    step.x = x;step.y = y;step.z = z;step.time = time;
                    q.push(step);
                }
            }
        }
    }
    int main()
    {
        while(scanf("%d%d%d",&l,&r,&c), l != 0 && r != 0 && c != 0)
        {
            memset(hash,0,sizeof(hash));
            for(int i = 1;i <= l;i ++)
              for(int j = 1;j<= r;j ++)
                 for(int k = 1;k <= c;k ++)
                 {
                        scanf(" %c",&map[i][j][k]);
                        if(map[i][j][k] == 'S')
                        {
                            sx = i;sy = j;sz = k;
                            hash[i][j][k] = 1;
                        }
                        if(map[i][j][k] == 'E')
                        {
                            ex = i;ey = j;ez = k;
                        }
                 }
                 BFS();
                 if(flag == 0)
                     printf("Trapped! ");
        }
        return 0;
    }
                
                        
                    
  • 相关阅读:
    【笔记篇】(理论向)快速傅里叶变换(FFT)学习笔记w
    【学术篇】bzoj2440 [中山市选2011]完全平方数
    【笔记篇】斜率优化dp(五) USACO08MAR土地购(征)买(用)Land Acquisition
    【笔记篇】斜率优化dp(四) ZJOI2007仓库建设
    【笔记篇】斜率优化dp(三) APIO2010特别行动队
    【笔记篇】斜率优化dp(二) SDOI2016征途
    【笔记篇】斜率优化dp(一) HNOI2008玩具装箱
    【笔记篇】单调队列优化dp学习笔记&&luogu2569_bzoj1855股票交♂易
    usr/include/php5/ext/pcre/php_pcre.h:29:18: fatal error: pcre.h 错误解决
    ubuntu 使用apt-get install 安装php5.6--php7
  • 原文地址:https://www.cnblogs.com/GODLIKEING/p/3306680.html
Copyright © 2011-2022 走看看