• BFS


    Dungeon Master

    题目链接:

    https://vjudge.net/problem/POJ-2251

    题目:

    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!
    

    思路:

    只是简单的从二维空间扩展到了三维的空间,方向变成 前 后 左 右 上 下 六个,图和标记数组都用三维数组

    多维数组:最右下标变化最快 

    用第一个下标表示层数然后是行和列

    代码:

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <queue>
    using namespace std;
    int l,r,c,vis[40][40][40];
    char pic[40][40][40];
    int dx[6]={1,-1,0,0,0,0};
    int dy[6]={0,0,1,-1,0,0};
    int dz[6]={0,0,0,0,1,-1};//六个方向
    struct node
    {
        int x,y,z,step;
    };
    void bfs(int xx,int yy,int zz)
    {
        memset(vis,0,sizeof(vis));
        queue<node>q;
        node b,now,next;
        b.x=xx;
        b.y=yy;
        b.z=zz;
        b.step=0;
        vis[xx][yy][zz]=1;
        q.push(b);
        while(!q.empty())
        {
            now=q.front();
            q.pop();
            if(pic[now.x][now.y][now.z]=='E')
            {
                printf("Escaped in %d minute(s).
    ",now.step);
                return;
            }
            for(int d=0;d<6;d++)
            {
                next=now;
                next.x+=dx[d];
                next.y+=dy[d];
                next.z+=dz[d];
                next.step++;
                if(next.x<0||next.y<0||next.z<0||next.x>=l||next.y>=r||next.z>=c)continue;//越界
                if(vis[next.x][next.y][next.z]==0&&pic[next.x][next.y][next.z]!='#')
                {
                    vis[next.x][next.y][next.z]=1;
                    q.push(next);
                }
            }
        }
        printf("Trapped!
    ");return;//找不到出口
    }
     
    int main()
    {
    //    freopen("in.txt","r",stdin);
        int bx,by,bz;
        while(scanf("%d%d%d",&l,&r,&c)!=EOF)
        {
            getchar();
            if(l==0||r==0||c==0)break;
            for(int i=0;i<l;i++)
            {
                for(int j=0;j<r;j++)
                {
                    for(int k=0;k<c;k++)
                        {
                            scanf("%c",&pic[i][j][k]);
                            if(pic[i][j][k]=='S'){bx=i;by=j;bz=k;}
                        }
                        getchar();     //因为只能遍历一遍不然会超时所以再输入的时候找到起始点
                }
                getchar();
            }
            bfs(bx,by,bz);
    //         for(int i=0;i<l;i++)   //用第二次遍历找起始点而超时
    //        {
    //            for(int j=0;j<r;j++)
    //            {
    //                for(int k=0;k<c;k++)
    //                printf("%c",pic[i][j][k]);
    //                printf("
    ");
    //            }
    //            printf("
    ");
    //        }
    //            for(int i=0;i<l;i++)
    //                for(int j=0;j<r;j++)
    //                for(int k=0;k<c;k++)
    //                if(pic[i][j][k]=='S')
    //            {
    //                bfs(i,j,k);break;
    //            }
     
        }
        return 0;
    }
  • 相关阅读:
    on、where、having的区别和关系
    Java知识点补缺
    Hive部署到IDEA报错 Hive Schema version 2.1.0 does not match metastore's schema version 1.2.0 Metastore is not upgraded or corrupt 解决方案
    Hive知识点总结
    区分同步与异步、阻塞与非阻塞
    Hive查询分区元数据,PARTITIONED BY
    单例模式总结
    Sql语句执行顺序
    收藏大数据相关面试题比较好的链接
    实习技能
  • 原文地址:https://www.cnblogs.com/20172674xi/p/9539791.html
Copyright © 2020-2023  润新知