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.
这个题只需要跑男孩和女朋友就行,直接判断与鬼的距离,不需要对鬼进行搜索
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
1
1
-1
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <queue>
using namespace std;
int off[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
char str[800][800];
int used[2][800][800];//标记是否有鬼
int n,m,step;
struct node
{
int x,y;
} start,zhan[2];
queue<node> q[2];
int judge(node b)
{
if(b.x<0 || b.y<0 || b.x>=n || b.y>=m)
return 0;
if(str[b.x][b.y]=='X')
return 0;
//曼哈顿距离判断问题,鬼不用广搜,直接判断距离即可
if((abs(b.x-zhan[0].x)+abs(b.y-zhan[0].y))<=2*step)
return 0;
if((abs(b.x-zhan[1].x)+abs(b.y-zhan[1].y))<=2*step)
return 0;
return 1;
}
int bfs(int w)
{
node next,now;
int sum=q[w].size();
while(sum--)
{
now=q[w].front();
q[w].pop();
if(judge(now)==0)
continue;
for(int i=0; i<4; i++)
{
next.x=now.x+off[i][0];
next.y=now.y+off[i][1];
if(judge(next)==0)
continue;
if(used[w][next.x][next.y]==0)
{
if(used[1-w][next.x][next.y]==1)
{
return 1;//会和成功
}
used[w][next.x][next.y]=1;
q[w].push(next);
}
}
}
return 0;
}
int solve()
{
step=0;
while((q[0].empty()==0) || (q[1].empty()==0))
{
step++;
if(bfs(0)==1)
{
return step;
}
if(bfs(0)==1)
{
return step;
}
if(bfs(0)==1)
{
return step;
}
if(bfs(1)==1)
{
return step;
}
}
return -1;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int i,j,cnt;
scanf("%d%d",&n,&m);
for(int i=0; i<n; i++)
scanf("%s",str[i]);
while(!q[0].empty())
q[0].pop();
while(!q[1].empty())
q[1].pop();
memset(used,0,sizeof(used));
cnt=0;
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
{
if(str[i][j]=='G')
{
start.x=i;
start.y=j;
q[1].push(start);
used[1][i][j]=1;
}
if(str[i][j]=='M')
{
start.x=i;
start.y=j;
q[0].push(start);
used[0][i][j]=1;
}
if(str[i][j]=='Z')
{
zhan[cnt].x=i;
zhan[cnt].y=j;
cnt++;
}
}
printf("%d\n",solve());
}
return 0;
}