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

    Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total
    Submissions: 38847 Accepted: 14802 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

    注意几点:
    搜索是从S开始而不是1,1,1
    多组输入要memset一下

    因为方向问题调了好久。。x,y,z一视同仁就挺好
    确实加深了对bfs的认识

    /* Writer:GhostCai && His Yellow Duck */
    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <queue>
    #define MAXN 31
    using namespace std;
    
    bool flag;
    
    struct point {
        int x, y, z;
    } node, r;
    
    char    a[MAXN][MAXN][MAXN];
    bool    vis[MAXN][MAXN][MAXN];
    point   pre[MAXN][MAXN][MAXN];
    int h, m, n;
    int mx[6] = { 1, 0, -1, 0, 0, 0 };
    int my[6] = { 0, 1, 0, -1, 0, 0 };
    int mz[6] = { 0, 0, 0, 0, 1, -1 };
    void print( point x )
    {
        flag = 1;
        int cnt = 0;
        point   u   = pre[x.x][x.y][x.z];
        while ( u.x != 0 && u.y != 0 && u.z != 0 )
        {
            u = pre[u.x][u.y][u.z];
            cnt++;
        }
        printf( "Escaped in %d minute(s).\n", cnt ); /* \n! */
        return;
    }
    
    
    void bfs( int x, int y, int z )
    {
        node.x      = 0;
        node.y      = 0;
        node.z      = 0;
        vis[x][y][z]    = 1;
        pre[x][y][z]    = node;
        node.x      = x; node.y = y; node.z = z;
        queue<point> Q;
        Q.push( node );
        while ( !Q.empty() && !flag )
        {
            r = Q.front();
            Q.pop();
            int i, j;
            int nx, ny, nz;
            for ( i = 0; i <= 5; i++ )
            {
                nx  = r.x + mx[i];
                ny  = r.y + my[i];
                nz  = r.z + mz[i];
                if ( nx < 1 || nx > m || ny < 1 || ny > n || nz < 1 || nz > h )
                    continue;
                if ( a[nx][ny][nz] == '#' || vis[nx][ny][nz] )
                    continue;
    /*              cout<<nx<<" "<<ny<<" "<<nz<<endl; */
                node.x  = nx;
                node.y  = ny;
                node.z  = nz;
                Q.push( node );
                pre[nx][ny][nz] = r;
                vis[nx][ny][nz] = 1;
                if ( a[nx][ny][nz] == 'E' )
                {
                    /* cout<<"SUCCESS"; */
                    print( node );
                }
            }
        }
    }
    
    
    int main()
    {
        while ( cin >> h >> m >> n )
        {
            int sx, sy, sz;
            if ( h == 0 && m == 0 && n == 0 )
                return(0);
            flag = 0;
            memset( a, 0, sizeof(a) );
            memset( vis, 0, sizeof(vis) );
            memset( pre, 0, sizeof(pre) );
            int i, j, k;
            for ( i = 1; i <= h; i++ )
            {
                for ( j = 1; j <= m; j++ )
                {
                    for ( k = 1; k <= n; k++ )
                    {
                        cin >> a[j][k][i]; /* -------------- */
                        if ( a[j][k][i] == 'S' )
                        {
                            sx  = j;
                            sy  = k;
                            sz  = i;
                        }
                    }
                }
            }
            bfs( sx, sy, sz );
            if ( !flag )
                cout << "Trapped!\n";
        }
    }

    本文来自博客园,作者:GhostCai,转载请注明原文链接:https://www.cnblogs.com/ghostcai/p/9247547.html

  • 相关阅读:
    android selector下的设置背景属性值
    成功必备的15种心态
    saveInstallState参数使用详解(android activity状态保存和恢复)
    14个坏习惯可能让你丢掉工作
    如何找到好书?有什么技巧或建议?
    [转]Git详解之一 Git起步
    程序员技术练级攻略
    Sina微博OAuth2框架解密
    程序员的八个级别
    Android中的Layout_weight详解
  • 原文地址:https://www.cnblogs.com/ghostcai/p/9247547.html
Copyright © 2011-2022 走看看