• 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队列容器+方向数组的运用+广度优先搜索(特殊情况的排查,不重不漏)

  • 相关阅读:
    google的开源项目总结
    Google Breakpad 完全解析(二) —— Windows前台实现篇
    Google Breakpad 完全解析(一) —— Windows入门篇
    PVS-Studio静态通用分析规则
    C,C++开源项目中的100个Bugs
    一日一点RakNet(3)--介绍
    C++开源跨平台类库集
    如何实现数组的随机排序?
    JavaScript原型,原型链 ? 有什么特点?
    js的基本数据类型
  • 原文地址:https://www.cnblogs.com/myxdashuaige/p/8781881.html
Copyright © 2020-2023  润新知