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

  • 相关阅读:
    unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type.
    Go的学习笔记之Channel发送和接收
    聊聊 PC 端自动化最佳方案
    一文彻底搞懂快速幂(原理实现、矩阵快速幂)
    一次core.<number> 文件占满磁盘空间的处理过程
    博文目录
    备忘录:C#获取微信小程序的云数据库中数据
    T-SQL——关于跨库连接查询
    .NET异步程序设计——给线程传递数据
    自研 Pulsar Starter:winfun-pulsar-spring-boot-starter
  • 原文地址:https://www.cnblogs.com/myxdashuaige/p/8781881.html
Copyright © 2011-2022 走看看