• 暑期第一弹<搜索> B


    B - Dungeon Master
    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    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!

    题意:有一个三维的空间,共l层,每层r行n列,#表示墙壁;有一头龙在S处,每一次他可以上下,前后,左右,六个方向移动。问它到达E点的最短步数。
    思路:最典型的bfs求最短路问题了,可以套着板子做了都,每次有6个状态,分别将6个状态加入队列就好。切记不可忘了边界判断条件。 理解bfs可以用扇形区域来理解,每次都一层一层向外扩散,像扇子一样,先出队列且为目标位置的点一定是最短路径到达的。
    再说一遍,利用树理解!
    代码如下:

    #include <iostream>
    #include <string>
    #include <cstring>
    #include <queue>
    using namespace std;
    
    struct Node{
        int x,y,z;
        int cont;
    };
    char Map[35][35][35];
    int visit[35][35][35];
    int dir[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
    int l,r,c;
    int x1,y1,z1;
    
    void bfs(){
        queue <Node> Q;
        Node t;
        t.x = x1,t.y = y1,t.z = z1,t.cont = 0;
        visit[x1][y1][z1] = 1;
        Q.push(t);
        while(!Q.empty()){
            Node res = Q.front();
            Q.pop();
    
            int xx = res.x;
            int yy = res.y;
            int zz = res.z;
            int Cont = res.cont;
    
            if(Map[xx][yy][zz] == 'E'){     //目标位置且一定最短
                cout<<"Escaped in "<<Cont<<" minute(s)."<<endl;
                return ;
            }
    
            for(int i = 0;i < 6;i ++){      //每一步6个状态
                Node temp;
                int xi = temp.x = xx+dir[i][0];
                int yi = temp.y = yy+dir[i][1];
                int zi = temp.z = zz+dir[i][2];
                temp.cont = Cont+1;
                if(xi < 1 || xi > l || yi < 1 || yi > r || zi < 1 || zi > c)    continue;       //边界判断
                if(Map[xi][yi][zi] != '#' && !visit[xi][yi][zi]){
                    visit[xi][yi][zi] = 1;
                    Q.push(temp);
                }
            }
        }
        cout<<"Trapped!"<<endl;
    }
    int main()
    {
        while(cin>>l>>r>>c){
            if(l == 0 && r == 0 && c == 0)  break;
            for(int i = 1;i <= l;i ++){
                for(int j = 1;j <= r;j ++){
                    for(int k = 1;k <= c;k ++){
                        cin>>Map[i][j][k];
                        if(Map[i][j][k] == 'S')
                            x1 = i,y1 = j,z1 = k;
                    }
                }
            }
            memset(visit,0,sizeof(visit));
            bfs();
        }
        return 0;
    }
    


  • 相关阅读:
    OSGi for C/C++
    Tizen NPPlugin开发
    Trove4j
    [Tizen]某些目录下存放的东西
    OpenMobile's Application Compatibility Layer (ACL)
    params
    页面无法访问
    websevice 服务前台和后台
    SQL 创建存储过程
    UpdatePanel
  • 原文地址:https://www.cnblogs.com/Jstyle-continue/p/6351942.html
Copyright © 2020-2023  润新知