[BFS] HDU 3085

来源:互联网 发布:爱淘宝的红包怎么用 编辑:程序博客网 时间:2024/06/06 01:58

输入n*m的字符矩阵,

矩阵中的M速度是3格/m,G的速度是1格/m,

Z是鬼,初始有两个,每秒可以变出很多的分身Z(变出的分身在下一秒钟仍然可以变出无数个分身),

占领跟Z距离是2的方格,直到占领所有的方格,

每次都是鬼先占领方格,然后是M跟G走,

M跟G可以同时都走,也可以有一个在原地不动,一个在走



不用做预处理,只要判断在固定时间内和鬼的曼哈顿距离就行了

两个人在当前时间内每走一步都判断一下是否能到达对方


#include <cmath>#include <cstdio>#include <cstring>#include <iostream>#include <queue>using namespace std;int n, m;int step; //当前走过了多少时间struct Node {        int x, y;} err, gf, ghost[ 2 ];int dir[ 4 ][ 2 ]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};char str[ 805 ][ 805 ];bool vis[ 2 ][ 805 ][ 805 ]; //双广,代表err和gfbool path ( int x, int y ) {        //是否出界或者撞墙        if ( x < 0 || x >= n || y < 0 || y >= m || str[ x ][ y ] == 'X' )                return false;        //是否在两个ghost的范围里面,曼哈顿距离        if ( ( abs ( x - ghost[ 0 ].x ) + abs ( y - ghost[ 0 ].y ) ) <= 2 * step )                return false;        if ( ( abs ( x - ghost[ 1 ].x ) + abs ( y - ghost[ 1 ].y ) ) <= 2 * step )                return false;        return true;}// err 和 gfqueue<Node> q[ 2 ];//是err 还是 gf 走//当前step数下做四个方向的搜索int bfs ( int w ) {        Node cur, nex;        int sum = q[ w ].size ();        //当前时间里有多少种走法        while ( sum-- ) {                cur = q[ w ].front ();                q[ w ].pop ();                if ( !path ( cur.x, cur.y ) )                        continue;                for ( int i = 0; i < 4; ++i ) {                        nex.x = cur.x + dir[ i ][ 0 ];                        nex.y = cur.y + dir[ i ][ 1 ];                        if ( path ( nex.x, nex.y ) && !vis[ w ][ nex.x ][ nex.y ] ) {                                //下一步找到了对方                                if ( vis[ w ^ 1 ][ nex.x ][ nex.y ] )                                        return 1;                                vis[ w ][ nex.x ][ nex.y ] = true;                                q[ w ].push ( nex );                        }                }        }        return -1;}int solve () {        while ( !q[ 0 ].empty () )                q[ 0 ].pop ();        while ( !q[ 1 ].empty () )                q[ 1 ].pop ();        memset ( vis, false, sizeof ( vis ) );        vis[ 0 ][ err.x ][ err.y ] = true;        vis[ 1 ][ gf.x ][ gf.y ] = true;        q[ 0 ].push ( err );        q[ 1 ].push ( gf );        step = 0;        while ( !q[ 0 ].empty () || !q[ 1 ].empty () ) {                ++step;                // err                if ( bfs ( 0 ) == 1 )                        return step;                if ( bfs ( 0 ) == 1 )                        return step;                if ( bfs ( 0 ) == 1 )                        return step;                // gf                if ( bfs ( 1 ) == 1 )                        return step;        }        return -1;}int main () {        int kcase;        scanf ( "%d", &kcase );        while ( kcase-- ) {                scanf ( "%d%d", &n, &m );                int cnt = 0;                for ( int i = 0; i < n; ++i ) {                        scanf ( "%s", str[ i ] );                        for ( int j = 0; j < m; ++j ) {                                if ( str[ i ][ j ] == 'M' )                                        err.x = i, err.y = j;                                else if ( str[ i ][ j ] == 'G' )                                        gf.x = i, gf.y = j;                                else if ( str[ i ][ j ] == 'Z' )                                        //下一个ghost就是cnt = 1                                        ghost[ cnt ].x = i, ghost[ cnt++ ].y = j;                        }                }                int sol = solve ();                printf ( "%d\n", sol );        }        return 0;}


原创粉丝点击