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;
    }
                
                        
                    
  • 相关阅读:
    假期训练七(hdu-2845 dp,hdu-1846,2188 巴什博奕)
    假期训练六(poj-1753,递归+hdu-2844,多重背包)
    假期训练五(poj-1077bfs+康拓展开,hdu-2577dp)
    1-10假期训练(hdu-2059 简单dp)
    1-9-假期训练心得(dp+bfs)
    如何在IntelliJ Idea中同时启动不同端口的两个实例
    搭建Springboot监控中心报错A attempt was made to call the method reactor.retry.Retry.retryMax(I)Lreactor/ret)
    使用spring中遇到"java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor"问题
    mvc:annotation-driven的前缀 "mvc"未绑定
    使用Idea构建springmvc框架,出现no bean named 'cacheManager' is defined 错误
  • 原文地址:https://www.cnblogs.com/GODLIKEING/p/3306680.html
Copyright © 2011-2022 走看看