# HDU3085 Nightmare Ⅱ[双向bfs]

D - Nightmare Ⅱ
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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

3 5 6 XXXXXX XZ..ZX XXXXXX M.G... ...... 5 6 XXXXXX XZZ..X XXXXXX M..... ..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

1 1 -1

M一次可以走三步 G只能走一步 鬼可以走两步

M的三步我用了一个leave去保存, 从0-2 到了2之后 就把该点的步数加一 放在队列里面

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
using namespace std;
const int N=810;
int n,m;
char Map[N][N];
bool mark[N][N][2],flag,used[N][N];
int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
struct node
{
int x,y,step,leave;
}M,G,Z[2];
queue<node>q;
queue<node>p;
bool check(int x,int y,int num,int step)
{
if (x<0 || x>=n || y<0 || y>=m || mark[x][y][num] || Map[x][y]=='X' || abs(x-Z[0].x)+abs(y-Z[0].y)<=2*step || abs(x-Z[1].x)+abs(y-Z[1].y)<=2*step)
return 0;
return 1;
}
bool check(int x,int y,int step)
{
if (abs(x-Z[0].x)+abs(y-Z[0].y)<=2*step || abs(x-Z[1].x)+abs(y-Z[1].y)<=2*step)
return 0;
return 1;
}
int bfs()
{
flag=0;
while (!q.empty())
q.pop();
while (!p.empty())
p.pop();
q.push(M);
p.push(G);
int s=0;
while (!q.empty() || !p.empty())
{
node start,later;
while (!q.empty() && q.front().step<=s)
{
start=q.front();
q.pop();
if (!check(start.x,start.y,start.step+1))
continue;
for (int i=0 ; i<4 ; i++)
{
int nx=start.x+dir[i][0];
int ny=start.y+dir[i][1];
if (check(nx,ny,0,start.step+1))
{
if (mark[nx][ny][1])
return start.step+1;
mark[nx][ny][0]=1;
later.x=nx;
later.y=ny;
later.step=start.step;
if (start.leave==2)
{
later.leave=0;
later.step++;
}
else
later.leave=start.leave+1;
q.push(later);
}
}
}
while (!p.empty() && p.front().step<=s)
{
start=p.front();
p.pop();
if (!check(start.x,start.y,start.step+1))
continue;
for (int i=0 ; i<4 ; i++)
{
int nx=start.x+dir[i][0];
int ny=start.y+dir[i][1];
if (check(nx,ny,1,start.step+1))
{
if (mark[nx][ny][0])
return start.step+1;
mark[nx][ny][1]=1;
later.x=nx;
later.y=ny;
later.step=start.step+1;
later.leave=start.leave;
p.push(later);
}
}
}
s++;
}
return -1;
}
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
scanf("%d%d",&n,&m);
memset(mark,0,sizeof(mark));
int z=0;
for (int i=0 ; i<n ; i++)
{
scanf("%s",Map[i]);
for (int j=0 ; j<m ; j++)
{
if (Map[i][j]=='M')
{
M.x=i;
M.y=j;
M.leave=0;
mark[i][j][0]=1;
M.step=0;
}
else if (Map[i][j]=='G')
{
G.x=i;
G.y=j;
G.leave=0;
mark[i][j][1]=1;
G.step=0;
}
else if (Map[i][j]=='Z')
{
Z[z].x=i;
Z[z].y=j;
Z[z++].step=0;
}
}
}
printf("%d\n",bfs());
}
return 0;
}

/*
5 6
XXXXXX
XZZ..X
XXXXXX
M.....
..G...
5 6
XXXXXX
XZZ..X
XXXXXX
M.....
.XG...
5 6
XXXXXX
XZZ..X
XXXXXX
MG....
.X....

5 6
XXXXXX
XZZ..X
XXXXXX
M.X...
XX.XG.

*/