HDOJ 3085 Nightmare Ⅱ

来源:互联网 发布:mysql实现读写分离 编辑:程序博客网 时间:2024/06/06 10:41

题意:在不被ghost(Z)捉到的情况下,G和M是否能够相遇,人只能走有路的地方,而ghost可以穿墙,G的移动速度为1,Z移动速度为2,M速度为3

链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085

思路:分别从G点和M点进行双向广度优先搜索,并且对于其中的每一个节点与Z点的Manhattan距离进行时间的计算。

注意点:无


以下为AC代码:

#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <vector>#include <deque>#include <list>#include <cctype>#include <algorithm>#include <climits>#include <queue>#include <stack>#include <cmath>#include <map>#include <set>#include <iomanip>#include <cstdlib>#include <ctime>#define RDI(a) scanf ( "%d", &a )#define RDII(a, b) scanf ( "%d%d", &a, &b )#define RDIII(a, b, c) scanf ( "%d%d%d", &a, &b, &c );#define RDS(s) scanf ( "%s", s );#define REP(i,m,n) for ( int i = m; i <= n; i ++ )#define DEP(i,m,n) for ( int i = m; i >= n; i -- )#define REPI(i,m,n,k) for ( int i = m; i <= n; i += k )#define DEPI(i,m,n,k) for ( int i = m; i >= n; i -= k )#define ll long long#define ull unsigned long long#define all(x) (x).begin(), (x).end()#define clr(a, v) memset( a , v , sizeof(a) )#define pb push_back#define read(f) freopen(f, "r", stdin)#define write(f) freopen(f, "w", stdout)using namespace std;//const double pi = acos(-1);const double eps = 1e-10;const int dir[4][2] = { 1,0, -1,0, 0,1, 0,-1 };bool adj[2][1005][1005];char str[1005][1005];struct node{    int x, y, cnt;    node(){}    node( int _x, int _y ) : x(_x), y(_y) {}    node( int _x, int _y, int _cnt ) : x(_x), y(_y), cnt(_cnt) {}};int m, n;node z[2];queue<node> q[2];node y;node g;int cnt;void init(){    clr ( adj, 0 );    clr ( str, 'X' );    cnt = 0;    int num = 0;    RDII ( m, n );    REP ( i, 1, m ){        RDS ( &str[i][1] );        str[i][n+1] = 'X';        REP ( j, 1, n ){            if ( str[i][j] == 'Z' )                z[num++] = node ( i, j );            if ( str[i][j] == 'M' )                y = node ( i, j );            if ( str[i][j] == 'G' )                g = node ( i, j );        }    }}void print(){    printf ( "\n" );    REP ( k, 0, 1 ){        REP ( i, 0, m + 1 ){            REP ( j, 0, n + 1 ){                printf ( "%d ", adj[k][i][j] );            }            printf ( "\n" );        }        printf ( "\n" );    }}bool judge ( node tmp ){    if ( tmp.x < 0 || tmp.x > m || tmp.y < 0 || tmp.y > n || str[tmp.x][tmp.y] == 'X' )return false;    REP ( i, 0, 1 ){        //printf ( "%d %d %d %d\n", tmp.x, tmp.y, abs ( tmp.x - z[i].x ) + abs ( tmp.y - z[i].y ), 2 * cnt );        if ( abs ( ( tmp.x - z[i].x ) ) + abs ( ( tmp.y - z[i].y ) ) <= 2 * cnt )            return false;    }    return true;}bool bfs ( int k ){    int s = q[k].size();    while ( s -- ){        node tmp = q[k].front();        //cout <<  k <<' ' << tmp.x << ' ' << tmp.y << endl;        q[k].pop();        if ( ! judge ( tmp ) )continue;        REP ( i, 0, 3 ){            int xi = tmp.x + dir[i][0];            int yi = tmp.y + dir[i][1];            if ( judge ( node ( xi, yi ) ) ){                if ( adj[k][xi][yi] )continue;                adj[k][xi][yi] = true;                if ( adj[k^1][xi][yi] )return true;                q[k].push ( node ( xi, yi ) );            }        }    }    return false;}bool solve(){    while ( ! q[0].empty() )q[0].pop();    while ( ! q[1].empty() )q[1].pop();    q[0].push ( y );    q[1].push ( g );    adj[0][y.x][y.y] = adj[1][g.x][g.y] = 1;    while ( ( ! q[0].empty() ) || ( ! q[1].empty() ) ){        cnt ++;        //REP ( i, 1, 3 ){            if ( bfs ( 0 ) )return true;            if ( bfs ( 0 ) )return true;            if ( bfs ( 0 ) )return true;        //}        if ( bfs ( 1 ) )return true;        //cout << cnt << endl;    }    return false;}int main(){    int t;    RDI(t);    while ( t -- ){        init();        if ( solve() )            printf ( "%d\n", cnt );        else            printf ( "-1\n" );        //print();    }    return 0;}


0 0
原创粉丝点击