[BFS] HDU 2102

来源:互联网 发布:淘宝店铺升级在哪里 编辑:程序博客网 时间:2024/05/17 07:15

两层迷宫,三维坐标表示

遇到上下两层都是# 的,就把上下两层的这个位置都弄成 墙就行

还有遇到 一层是#一层是墙的。。也直接把俩都弄城墙就行。。。


#include <cstdio>#include <cstring>#include <iostream>#include <queue>using namespace std;struct Node {        int x, y, z;        int cnt;};int n, m, t;//两层char maze[ 2 ][ 35 ][ 35 ];bool vis[ 2 ][ 35 ][ 35 ];int dir[ 4 ][ 2 ] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};bool path ( int x, int y, int z ) {        if ( x < 0 || x >= n || y < 0 || y >= m || maze[ z ][ x ][ y ] == '*' )                return false;        //进不了的入口直接屏蔽掉        if ( maze[ z ][ x ][ y ] == '#' &&             ( maze[ !z ][ x ][ y ] == '#' || maze[ !z ][ x ][ y ] == '*' ) )                return false;        return true;}bool bfs () {        Node cur, nex;        cur.x = cur.y = cur.z = 0;        cur.cnt = 0;        vis[ cur.z ][ cur.x ][ cur.y ] = true;        queue<Node> Q;        Q.push ( cur );        while ( !Q.empty () ) {                cur = Q.front ();                Q.pop ();                //超过时限                if ( cur.cnt > t )                        return false;                //找到结果                if ( maze[ cur.z ][ cur.x ][ cur.y ] == 'P' ) {                        return true;                }                for ( int i = 0; i < 4; ++i ) {                        nex.x = cur.x + dir[ i ][ 0 ];                        nex.y = cur.y + dir[ i ][ 1 ];                        nex.z = cur.z;                        if ( path ( nex.x, nex.y, nex.z ) && !vis[ nex.z ][ nex.x ][ nex.y ] ) {                                if ( maze[ nex.z ][ nex.x ][ nex.y ] ==                                     '#' ) //找到传送站直接换到另一层,不算时间                                        nex.z = !cur.z;                                nex.cnt = cur.cnt + 1;                                vis[ nex.z ][ nex.x ][ nex.y ] = true;                                Q.push ( nex );                        }                }        }        return false;}int main () {        int c;        scanf ( "%d", &c );        while ( c-- ) {                scanf ( "%d%d%d", &n, &m, &t );                memset ( vis, false, sizeof ( vis ) );                for ( int i = 0; i < n; ++i )                        scanf ( "%s", maze[ 0 ][ i ] );                for ( int i = 0; i < n; ++i )                        scanf ( "%s", maze[ 1 ][ i ] );                bool flag = bfs ();                if ( flag )                        printf ( "YES\n" );                else                        printf ( "NO\n" );        }}


原创粉丝点击