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!


    就是六个方向的广搜,理清楚就好做
    #include<iostream>
    #include<cstring>
    #include<queue>
    using namespace std;
    int a,b,c;
    char s[30][30][30];
    int map[30][30][30];
    int next[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
    int si,sj,sk,ei,ej,ek;
    struct node
    {
        int x,y,z;
        int step;
    };
    int check(int x,int y,int z)
    {
        if(x<0 || y<0 || z<0 || x>=a || y>=b || z>=c)
            return 1;
        else if(s[x][y][z] == '#')
            return 1;
        else if(map[x][y][z])
            return 1;
        return 0;
    }
    int bfs()
    {
        queue <node> q;
        node t,no;
        t.x=si;t.y=sj;t.z=sk;
        t.step=0;
        map[si][sj][sk]=1;
        q.push(t);
        while(!q.empty())
        {
            t=q.front();
            q.pop();
            if(t.x == ei && t.y == ej && t.z == ek)
                return t.step;
            for(int i = 0; i<6; i++)
            {
                no = t;
                no.x = t.x+next[i][0];
                no.y = t.y+next[i][1];
                no.z = t.z+next[i][2];
                if(check(no.x,no.y,no.z))
                    continue;
                map[no.x][no.y][no.z] = 1;
                no.step = t.step+1;
                q.push(no);
            }
        }
        return 0;
    }
    int main()
    {
        while(cin>>a>>b>>c)
        {
            if(a==0&&b==0&&c==0)
                break;
            for(int i=0;i<a;i++)
            {
                for(int j=0;j<b;j++)
                {
                    for(int k=0;k<c;k++)
                    {
                        cin>>s[i][j][k];
                        if(s[i][j][k]=='S')
                        {
                            si=i;
                            sj=j;
                            sk=k;
                        }
                        if(s[i][j][k]=='E')
                        {
                            ei=i;
                            ej=j;
                            ek=k;
                        }
                    }
                }
            }
            memset(map,0,sizeof(map));
            int ans=bfs();
            if(ans)
                cout<<"Escaped in "<<ans<<" minute(s)."<<endl;
            else
                cout<<"Trapped!"<<endl;
        }
        return 0;
    }
    

      



  • 相关阅读:
    【洛谷3214】[HNOI2011] 卡农(思维)
    【洛谷2609】[ZJOI2012] 数列(高精度)
    【洛谷4501】[ZJOI2018] 胖(二分+RMQ)
    【洛谷4726】【模板】多项式指数函数(多项式 exp)
    uC/OS-II之入门与介绍20160525
    [转]Delphi 关键字详解
    [转]单元文件结构
    Delphi ComboBox的属性和事件、及几个鼠标事件的触发
    Delphi 用ToolButton和MonthCalendar实现DateTimePicker的功能
    Delphi 动态改变Rzsplitter的Orientation(方向)属性
  • 原文地址:https://www.cnblogs.com/zzzying/p/7236505.html
Copyright © 2011-2022 走看看