zoukankan      html  css  js  c++  java
  • POJ 2251 Dungeon Master(三维BFS)

    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!
    题解:
      
    直接BFS,和二维一样,除了前后左右,还多了上下的选择。
    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<cstring>
    using namespace std;
    char maze[40][40][40];
    bool vis[40][40][40];
    int s,m,n,es,ex,ey;
    int d[6][3]={{1,0,0},{-1,0,0},{0,0,1},{0,0,-1},{0,1,0},{0,-1,0}};
    struct node
    {
        int k,x,y,step;
    };
    void bfs(int k,int x,int y)
    {
        queue<node> que;
        node cur;
        cur.k=k,cur.x=x,cur.y=y,cur.step=0;
        que.push(cur);
        while(que.size())
        {
            cur=que.front();
            que.pop();
            if(cur.k==es&&cur.x==ex&&cur.y==ey)
            {
                printf("Escaped in %d minute(s).
    ",cur.step);
                return ;
            }
            for(int i=0;i<6;i++)
            {
                node next=cur;
                next.k+=d[i][0],next.x+=d[i][1],next.y+=d[i][2];
                if(0<=next.k&&next.k<s&&0<=next.x&&next.x<n&&0<=next.y&&next.y<m&&maze[next.k][next.x][next.y]!='#'&&!vis[next.k][next.x][next.y])
                {
                    vis[next.k][next.x][next.y]=true;
                    next.step++;
                    que.push(next);
                }
            }
        }
        cout<<"Trapped!"<<endl;
    }
    int main()
    {
        while(cin>>s>>n>>m,s||n||m)
        {
            memset(vis,false,sizeof(vis));
            int ss,sx,sy;
            for(int p=0;p<s;p++)
            {
                for(int i=0;i<n;i++)
                    for(int j=0;j<m;j++)
                    {
                        cin>>maze[p][i][j];
                        if(maze[p][i][j]=='S')
                            ss=p,sx=i,sy=j;
                        if(maze[p][i][j]=='E')
                            es=p,ex=i,ey=j;
                    }
            }
            vis[ss][sx][sy]=true;
            bfs(ss,sx,sy);
        }
        return 0;
    }
    
    
  • 相关阅读:
    未定义的标示符“RECT”,引入了windows.h头文件也没有用?
    解决Opencv高低版本不兼容问题
    在OpenCV2.2后的版本中没有CvvImage类的解决方法(及出现错误:IntelliSense: 未定义标识符 "CvvImage" )
    opencv中Mat与IplImage,CVMat类型之间转换
    opencv中VideoCapture和cvCapture有什么区别?
    2019-2020-1 20175302_20175314_20175316 实验三 并发程序
    2019-2020-1 20175314 《信息安全系统设计基础》第8周学习总结
    2019-2020-1 20175302_20175314_20175316 实验二 固件程序设计
    2019-2020-1 20175302_20175314_20175316 实验一 开发环境的熟悉
    *2019-2020-1 20175302_20175314_20175316 实验一 开发环境的熟悉*
  • 原文地址:https://www.cnblogs.com/orion7/p/7339148.html
Copyright © 2011-2022 走看看