zoukankan      html  css  js  c++  java
  • 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 <cstring>
    #include <map>
    #include <cmath>
    #include <string>
    #include <queue>
    using namespace std;
    class que
    {
        public:
        int x,y,z,c;
    }temp;
    char mp[30][30][30];
    int vis[30][30][30];
    int n,m,o,x,y,z,tx,ty,tz;
    int dir[6][3]={0,0,1,0,0,-1,0,1,0,0,-1,0,1,0,0,-1,0,0};
    int main()
    {
        queue<que>q;
        while(cin>>n>>m>>o)
        {
            if(!n&&!m&&!o)break;
            int flag=0;
            memset(vis,0,sizeof(vis));
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                {
                    for(int k=0;k<o;k++)
                    {
                        cin>>mp[i][j][k];
                        if(mp[i][j][k]=='S')x=i,y=j,z=k;
                    }
                }
            }
    
            while(!q.empty())q.pop();
            temp.x=x,temp.y=y,temp.z=z,temp.c=0,vis[x][y][z]=1;
            q.push(temp);
            while(!q.empty())
            {
                if(mp[q.front().x][q.front().y][q.front().z]=='E')
                {
                    flag=1;
                    printf("Escaped in %d minute(s).
    ",q.front().c);
                    break;
                }
                x=q.front().x,y=q.front().y,z=q.front().z;
                for(int i=0;i<6;i++)
                {
                    tx=x+dir[i][0],ty=y+dir[i][1],tz=z+dir[i][2];
                    if(tx<0||ty<0||tz<0||tx>=n||ty>=m||tz>=o||vis[tx][ty][tz]||mp[tx][ty][tz]=='#')continue;
                    vis[tx][ty][tz]=1;
                    temp.x=tx,temp.y=ty,temp.z=tz,temp.c=q.front().c+1;
                    q.push(temp);
                }
                q.pop();
            }
    
            if(!flag)cout<<"Trapped!"<<endl;
        }
    }
  • 相关阅读:
    关键字--static
    java注解
    服务器、应用服务器、web服务器、容器
    进程和线程
    Tomcat7目录结构详解(非常详细)
    HTML小练习
    HTML学习笔记
    javaoo总结二
    javaoo总结一
    python核心-类-1
  • 原文地址:https://www.cnblogs.com/8023spz/p/7247759.html
Copyright © 2011-2022 走看看