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;
    }
    
    
  • 相关阅读:
    jmeter非GUI模式命令
    jmeter性能测试--浪涌测试
    性能测试之场景设计
    性能测试用例实例
    jmeter常见错误及解决方法
    .NET中变量生存期
    SQL数据库从高版本导入低版本
    对称子字符串
    回溯法求解全排列问题(可去除重复排列)
    快速排序及快速选择问题
  • 原文地址:https://www.cnblogs.com/orion7/p/7339148.html
Copyright © 2011-2022 走看看