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
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
大致题意
t 组数据,n 行m 列,M是男朋友,一次可以走 3 格,G是女朋友,一次可以走 1 格,Z是鬼,一次走 2 格,X是墙,人不能走鬼可以哦,问男女朋友是否可以遇到彼此(鬼先走)
具体思路
鬼先进队列,后两个人,双向bfs,开个矩阵分别记录两个人的时间,注意,男生时间每3归1,每个bfs连续走三次就是一个dfs罢了
加粗样式
#include<bits/stdc++.h>
using namespace std;
int times[2][805][805];
char import[805][805];
int changex[4]={0,0,-1,1};
int changey[4]={-1,1,0,0};
int n,m,flag;
struct node
{
int x,y;
char opt;
};
deque<node>dui;
void dfs(int x,int y,char opt,int v)
{
if (v==0)
{
return ;
}else if (opt=='Z'&&flag==0)
{
for (int i=0;i<4;i++)
{
int xx=x+changex[i];
int yy=y+changey[i];
if (flag==0&&xx>=0&&xx<n&&yy>=0&&yy<m&&import[xx][yy]!='Z')
{
import[xx][yy]='Z';
node now;
now.x=xx;
now.y=yy;
now.opt='Z';
dui.push_back(now);
dfs(xx,yy,opt,v-1);
}
}
}else if (opt=='M'&&flag==0&&import[x][y]!='Z')
{
for (int i=0;i<4;i++)
{
int xx=x+changex[i];
int yy=y+changey[i];
if (flag==0&&xx>=0&&xx<n&&yy>=0&&yy<m&&import[xx][yy]!='Z'&&import[xx][yy]!='X'&×[0][xx][yy]>times[0][x][y]+1)
{
times[0][xx][yy]=times[0][x][y]+1;
node now;
now.x=xx;
now.y=yy;
now.opt='M';
dui.push_back(now);
if (times[1][xx][yy]!=640005)
{
if (times[0][xx][yy]%3==0)
{
printf("%d\n",times[0][xx][yy]/3);
}else
{
printf("%d\n",times[0][xx][yy]/3+1);
}
flag=1;
return ;
}
dfs(xx,yy,opt,v-1);
}
}
}else if (opt=='G'&&flag==0&&import[x][y]!='Z')
{
for (int i=0;i<4;i++)
{
int xx=x+changex[i];
int yy=y+changey[i];
if (flag==0&&xx>=0&&xx<n&&yy>=0&&yy<m&&import[xx][yy]!='Z'&&import[xx][yy]!='X'&×[1][xx][yy]>times[1][x][y]+1)
{
times[1][xx][yy]=times[1][x][y]+1;
node now;
now.x=xx;
now.y=yy;
now.opt='G';
if (times[0][xx][yy]!=640005)
{
printf("%d\n",times[1][xx][yy]);
flag=1;
return ;
}
dui.push_back(now);
}
}
}
return ;
}
int main()
{
int t;
scanf("%d%*c",&t);
while (t--)
{
while (!dui.empty())
{
dui.pop_back();
}
fill(times[0][0],times[0][0]+2*805*805,640005);
int mx,my,gx,gy;
flag=0;
scanf("%d %d",&n,&m);
for (int i=0; i<n; i++)
{
scanf("%s%*c",&import[i]);
for (int j=0; j<m; j++)
{
if (import[i][j]=='Z')
{
node z;
z.x=i;
z.y=j;
z.opt='Z';
dui.push_back(z);
}
if (import[i][j]=='M')
{
mx=i;
my=j;
times[0][i][j]=0;
}
if (import[i][j]=='G')
{
gx=i;
gy=j;
times[1][i][j]=0;
}
}
}
node z;
z.x=mx;
z.y=my;
z.opt='M';
dui.push_back(z);
z.x=gx;
z.y=gy;
z.opt='G';
dui.push_back(z);
while (!dui.empty())
{
node now;
now=dui.front();
dui.pop_front();
if (now.opt=='Z'&&flag==0)
{
dfs(now.x,now.y,now.opt,2);
if (flag==1)
{
break;
}
}
else if (now.opt=='M'&&flag==0)
{
if (import[now.x][now.y]!='Z')
{
dfs(now.x,now.y,now.opt,3);
if (flag==1)
{
break;
}
}
}
else if (now.opt=='G'&&flag==0)
{
if (import[now.x][now.y]!='Z')
{
dfs(now.x,now.y,now.opt,1);
if (flag==1)
{
break;
}
}
}
}
if (flag==0)
{
printf("-1\n");
}
}
return 0;
}