题目链接:SDTBU-ACM集训队暑期集训---第一讲 - Virtual Judge
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
Output:
1
1
-1
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define M 1001
char a[M][M];
int n,m,sum,vis[M][M][2],gx[2],gy[2];
int direction[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
struct Node
{
int x, y;
} node,temp;
queue<Node> q[2];
int judge(int x,int y)
{
if(abs(x-gx[0])+abs(y-gy[0])<=2*sum||abs(x-gx[1])+abs(y-gy[1])<=2*sum) return 1;
else return 0;
}
int bfs(int mg)
{
int i=q[mg].size();
while(i--)
{
temp=q[mg].front();
q[mg].pop();
if(judge(temp.x,temp.y)) continue;//判断鬼是否可以到达,如果可以该位置走不通,走下一个位置
for (int i=0; i<4; i++)//四个方向遍历
{
int dx=temp.x+direction[i][0],dy=temp.y+direction[i][1];
if(judge(dx,dy)) continue;//新位置判断
if (vis[dx][dy][mg^1]) return 1;//已走过可以相遇返回1
if(a[dx][dy]=='.'&&dx>0&&dx<=n&&dy>0&&dy<=m&&!vis[dx][dy][mg])
{
vis[dx][dy][mg] = 1;
node.x=dx,node.y=dy;
q[mg].push(node);
}
}
}
return 0;
}
int solve()
{
sum=0;
while(q[0].size()&&q[1].size())
{
sum++;
int i=3;
while(i--) if(bfs(0)) return sum;//man走三步
if(bfs(1)) return sum;
}
return -1;
}
int main()
{
int t,i,j;
cin>>t;
while (t--)
{
int f1=0,f2=0,f3=0;
memset(vis,0,sizeof(vis));
q[0]=q[1]=queue<Node>();//清队列
cin>>n>>m;
int cnt=0;
for (i=1; i<=n; i++)
{
cin>>a[i]+1;
for (j=1; j<=m&&(!f1||!f2||!f3); j++)
{
if (a[i][j]=='M'&&!f1)//标记man位置
{
node.x=i,node.y=j;
q[0].push(node);
vis[i][j][0]=1;
f1=1;
}
else if (a[i][j]=='G'&&!f2)
{
node.x=i,node.y=j;
q[1].push(node);
vis[i][j][1]=1;
f2=1;
}
else if(a[i][j]=='Z'&&!f3)
{
gx[cnt]=i;
gy[cnt++]=j;
if(cnt==2) f3=1;
}
}
}
cout<<solve()<<endl;
}
return 0;
}