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;
    }
                
                        
                    
  • 相关阅读:
    Python DayDayUp系列 —— 字符串操作(一)
    Condition对象以及ArrayBlockingQueue阻塞队列的实现(使用Condition在队满时让生产者线程等待, 在队空时让消费者线程等待)
    ReadWriteLock: 读写锁
    优秀的github项目学习
    synchronized:内部锁
    ReentreantLock:重入锁
    好的文章
    Java内存模型与volatile关键字
    GitHub远程库的搭建以及使用
    使用Executor框架创建线程池
  • 原文地址:https://www.cnblogs.com/GODLIKEING/p/3306680.html
Copyright © 2011-2022 走看看