HDU 3081 Nightmare Ⅱ(双向BFS)

来源:互联网 发布:上海新浪集团网络运营 编辑:程序博客网 时间:2024/05/07 21:10
Problem Description
Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were trapped in a big maze separately. More terribly, there are two ghosts in the maze. They will kill the people. Now little erriyue wants to know if he could find his girl friend before the ghosts find them.
You may suppose that little erriyue and his girl friend can move in 4 directions. In each second, little erriyue can move 3 steps and his girl friend can move 1 step. The ghosts are evil, every second they will divide into several parts to occupy the grids within 2 steps to them until they occupy the whole maze. You can suppose that at every second the ghosts divide firstly then the little erriyue and his girl friend start to move, and if little erriyue or his girl friend arrive at a grid with a ghost, they will die.
Note: the new ghosts also can devide as the original ghost.
 

Input
The input starts with an integer T, means the number of test cases.
Each test case starts with a line contains two integers n and m, means the size of the maze. (1<n, m<800)
The next n lines describe the maze. Each line contains m characters. The characters may be:
‘.’ denotes an empty place, all can walk on.
‘X’ denotes a wall, only people can’t walk on.
‘M’ denotes little erriyue
‘G’ denotes the girl friend.
‘Z’ denotes the ghosts.
It is guaranteed that will contain exactly one letter M, one letter G and two letters Z. 
 

Output
Output a single integer S in one line, denotes erriyue and his girlfriend will meet in the minimum time S if they can meet successfully, or output -1 denotes they failed to meet.
 

Sample Input
35 6XXXXXXXZ..ZXXXXXXXM.G.........5 6XXXXXXXZZ..XXXXXXXM.......G...10 10............X.........M.X...X.X..........X..X.X.X..........X..XX....X.X....G...X...ZX.X......Z..X..X
 

Sample Output
11-1
这题由于两个人都在移动所以要双向BFS.做法如下:特别的对于怪物我们可以用时间来
看待它影响的范围。于是此题就成了简单的BFS;
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<bitset>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair<int,int>pil;const int maxn = 805;const int mod = 1000000007;int dr[][2]={-1,0,1,0,0,-1,0,1};int mp[maxn][maxn];bool vis[2][maxn][maxn];typedef struct Node{    int x,y;}Node;queue<Node>q[2];Node G[2];int t,n,m,step;int sx,sy,ex,ey;bool ok(int x,int y){    if(x<0||x>=n||y<0||y>=m||mp[x][y]==1)  return false;    REP(i,2)        if(abs(x-G[i].x)+abs(y-G[i].y)<=2*step)  return false;    return true;}bool BFS(int num){    Node st,ed;    int size=q[num].size();    while(size--)    {        st=q[num].front();        q[num].pop();        int x=st.x,y=st.y;        if(!ok(x,y))  continue;        REP(i,4)        {            int xx=x+dr[i][0];            int yy=y+dr[i][1];            if(!ok(xx,yy))  continue;            if(vis[num][xx][yy])  continue;            if(vis[num^1][xx][yy])  return true;            ed.x=xx;ed.y=yy;            vis[num][xx][yy]=true;            q[num].push(ed);        }    }    return false;}int solve(){    Node st1,st2;    CLEAR(vis,false);    while(!q[0].empty())        q[0].pop();    while(!q[1].empty())        q[1].pop();    st1.x=sx;st1.y=sy;    q[0].push(st1);    st2.x=ex;st2.y=ey;    q[1].push(st2);    vis[0][sx][sy]=vis[1][ex][ey]=true;    step=0;    while(!q[0].empty()||!q[1].empty())    {        step++;        REP(i,3)           if(BFS(0))   return step;        if(BFS(1))  return step;    }    return -1;}int main(){    char str[1100];    scanf("%d",&t);    while(t--)    {        CLEAR(mp,0);        scanf("%d%d",&n,&m);        getchar();        int cnt=0;        REP(i,n)        {            gets(str); //           cout<<"fuck  "<<str<<endl;            REP(j,m)            {                if(str[j]=='X')                    mp[i][j]=1;                else if(str[j]=='Z')                {                    mp[i][j]=1;                    G[cnt].x=i;                    G[cnt++].y=j;                }                else if(str[j]=='M')                {                    mp[i][j]=0;                    sx=i;sy=j;                }                else if(str[j]=='G')                {                    mp[i][j]=0;                    ex=i;ey=j;                }                else   mp[i][j]=0;            }        }//        cout<<"fuck  "<<sx<<" "<<sy<<" "<<ex<<" "<<ey<<endl;        printf("%d\n",solve());    }    return 0;}


0 0
原创粉丝点击