zoukankan      html  css  js  c++  java
  • POJ 2251 Dungeon Master

    对于初学搜索问题比较有挑战性的一个问题:

    description:

    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!

    AC Code:

    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<cstring>
    using namespace std;
    char mp[35][35][35];
    bool flag[35][35][35];
    int way[6][3]={{1,0,0},{0,1,0},{0,0,1},{-1,0,0},{0,-1,0},{0,0,-1}};
    int ans,judge;
    int l,r,c;
    struct node
    {
        int x;
        int y;
        int z;
        int cnt;
    };
    
    void bfs(int x,int y,int z)
    {
        node q,ne;
        queue<node> G;
        q.cnt=0;q.x=x;q.y=y;q.z=z;
        G.push(q);
        while(G.size())
        {
            q=G.front();
            G.pop();
            for(int i=0;i<6;i++)
            {
                ne.x=q.x+way[i][0];
                ne.y=q.y+way[i][1];
                ne.z=q.z+way[i][2];
                ne.cnt=q.cnt+1;
    
                if(ne.x<0||ne.x>=l||ne.y<0||ne.y>=r||ne.z<0||ne.z>=c||mp[ne.x][ne.y][ne.z]=='#') continue;
    
                if(!flag[ne.x][ne.y][ne.z])
                {
                    if(mp[ne.x][ne.y][ne.z]=='E')
                    {
                        ans=ne.cnt;
                        judge=1;
                        return ;
                    }
                    flag[ne.x][ne.y][ne.z]=true;
                    G.push(ne);
                }
            }
        }
    }
    
    int main()
    {
        int o,p,q;
        while(scanf("%d %d %d",&l,&r,&c)!=EOF&&l&&c&&r)
        {
            memset(mp,'#',sizeof(mp));
            memset(flag,false,sizeof(flag));
            ans=0;judge=0;
    
            for(int i=0;i<l;i++)
            {
                for(int j=0;j<r;j++)
                {
                    for(int k=0;k<c;k++)
                    {
                        cin>>mp[i][j][k];
                        if(mp[i][j][k]=='S')
                        {
                            o=i;p=j;q=k;
                        }
                    }
                }
            }
            bfs(o,p,q);
            if(judge==1)  printf("Escaped in %d minute(s).
    ",ans);
            else  cout<<"Trapped!"<<endl;
        }
        return 0;
    }

    TIP:

    结构体+queue队列容器+方向数组的运用+广度优先搜索(特殊情况的排查,不重不漏)

  • 相关阅读:
    MySQL总结二
    MySQL总结一
    Flink 流处理API之Join
    Flink 流处理API之二
    Flink 流处理API之一
    Flink ProcessFunction API
    Flink Window
    Spark性能优化
    BPM软件_K2百家讲坛 | 越秀地产:K2为房企数字化转型带来更多可能_全球领先的工作流引擎
    BPM软件_K2签约龙光地产,为集团实现“千亿目标”保驾护航_全业务流程管理专家
  • 原文地址:https://www.cnblogs.com/myxdashuaige/p/8781881.html
Copyright © 2011-2022 走看看