zoukankan      html  css  js  c++  java
  • Dungeon Master 分类: 搜索 POJ 2015-08-09 14:25 4人阅读 评论(0) 收藏

    Dungeon Master
    Time Limit: 1000MS Memory Limit: 65536K
    Total Submissions: 20995 Accepted: 8150

    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!

    Source
    Ulm Local 1997
    三维的搜索,开始少算了时间复杂度,用dfs超时,果断的改成bfs

    #include <map>
    #include <list>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <string>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    #define eps 1e-9
    #define LL long long
    #define PI acos(-1.0)
    #define INF 0x3f3f3f3f
    #define CRR fclose(stdin)
    #define CWW fclose(stdout)
    #define RR freopen("input.txt","r",stdin)
    #define WW freopen("output.txt","w",stdout)
    
    const int MAX = 10000;
    
    struct node
    {
        int x;
        int y;
        int z;
    };
    
    struct point
    {
        int num;
        node a;
    };
    
    int L,R,C;
    
    int Dir[][3]= {{0,1,0},{0,-1,0},{1,0,0},{-1,0,0},{0,0,1},{0,0,-1}};
    
    char Map[35][35][35];
    
    node s,e;
    
    bool vis[35][35][35];
    
    int Max;
    
    bool Judge(int x,int y,int z)
    {
        if(x<R&&x>=0&&y<C&&y>=0&&z<L&&z>=0&&vis[z][x][y]==false&&Map[z][x][y]!='#')
        {
            return true;
        }
        return false;
    }
    
    void bfs()
    {
        point b,c;
        queue<point>Q;
        memset(vis,false,sizeof(vis));
        b.a.x=s.x;
        b.a.y=s.y;
        b.a.z=s.z;
        b.num=0;
        vis[s.z][s.x][s.y]=true;
        Q.push(b);
        while(!Q.empty())
        {
            c=Q.front();
            Q.pop();
            if(c.a.x==e.x&&c.a.y==e.y&&c.a.z==e.z)
            {
                Max=c.num;
                return ;
            }
            for(int i=0;i<6;i++)
            {
                b.a.x=c.a.x+Dir[i][0];
                b.a.y=c.a.y+Dir[i][1];
                b.a.z=c.a.z+Dir[i][2];
                b.num=c.num+1;
                if(Judge(b.a.x,b.a.y,b.a.z))
                {
                    vis[b.a.z][b.a.x][b.a.y]=true;
                    Q.push(b);
                }
    
            }
        }
    
    }
    int main()
    {
        while(~scanf("%d %d %d",&L,&R,&C))
        {
            if(L==0&&R==0&&C==0)
            {
                break ;
            }
            for(int i=0; i<L; i++)
            {
                for(int j=0; j<R; j++)
                {
                    scanf("%s",Map[i][j]);
                    for(int k=0; k<C; k++)
                    {
                        if(Map[i][j][k]=='S')
                        {
                            s.x=j;
                            s.y=k;
                            s.z=i;
                        }
                        if(Map[i][j][k]=='E')
                        {
                            e.x=j;
                            e.y=k;
                            e.z=i;
                        }
                    }
                }
            }
            Max = INF;
            bfs();
            if(Max<INF)
            {
                printf("Escaped in %d minute(s).
    ",Max);
            }
            else
                printf("Trapped!
    ");
        }
        return 0;
    }
    

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    Delphi TStringList的用法
    Android Studio使用教程(一)
    如何在win7下安装和配置Android Studio
    python基础字符串单引号双引号和三引号
    同步和互斥
    posix多线程有感线程高级编程(条件变量)
    VMware网络配置详解
    posix多线程有感POSIX 线程间的内存可视性
    进程及相关API
    POSIX线程属性
  • 原文地址:https://www.cnblogs.com/juechen/p/4721908.html
Copyright © 2011-2022 走看看