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
题意:两个人,他们想在某家KCF见面,要求他们到这家KCF的时间花费最少,KFC可以当做路,每一步花费11时刻。
思路:两个人,两次广搜,我们在简单的BFS上用一个数组mark记录两个人在@点花费的时间和,到最后求哪个@点的和最小即可;
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
struct node{
int x,y,t;
};
int vis[4][2]={0,1,0,-1,1,0,-1,0};
char map[250][250];
int mark1[250][250],mark2[250][250],mark[250][250];
int n,m;
int ok(int x,int y)
{
if(x>=1&&x<=n&&y>=1&&y<=m&&map[x][y]!='#')
return 1;
else
return 0;
}
void bfs(int x,int y)
{
memset(mark1,0,sizeof(mark1));
mark1[x][y]=1;
node q,p;
q.x=x,q.y=y,q.t=0;
queue<node>que;
que.push(q);
while(que.size())
{
q=que.front();
que.pop();
if(map[q.x][q.y]=='@')
{
mark[q.x][q.y]+=q.t;
}
for(int i=0;i<=3;i++)
{
p.x=q.x+vis[i][0];
p.y=q.y+vis[i][1];
if(ok(p.x,p.y)&&!mark1[p.x][p.y])
{
p.t=q.t+1;
mark1[p.x][p.y]=1;
que.push(p);
}
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
int zx,zy,dx,dy;
for(int i=1;i<=n;i++)
scanf("%s",map[i]+1);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(map[i][j]=='Y')
dx=i,dy=j;
else if(map[i][j]=='M')
zx=i,zy=j;
memset(mark2,0,sizeof(mark2));
memset(mark,0,sizeof(mark));
bfs(dx,dy);
bfs(zx,zy);
int maxn=9999999;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(maxn>mark[i][j]&&mark[i][j]!=0)
maxn=mark[i][j];
maxn=11*maxn;
printf("%d\n",maxn);
}
}