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

题目链接

有两个人分别在地图的不同位置,一个每个时间单位内可以走三步(或者说一步三格),另一个每个时间单位内可以走一步(或者说一步一格),地图上还有两个鬼魂,鬼魂每个时间单位内会向外扩散两格(可以穿墙),求两人相遇的最短时间。

双向bfs(宽度优先搜索),每次搜索更新一个时间单位,先判断与鬼魂的曼哈顿距离,之后记录两人分别可以到达的点位,若此点已被另一人标记则可以相遇,记录结果。

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define mp make_pair
#define lowbit(x) (x&(-x))
#define XDebug(x) cout<<#x<<"="<<x<<endl;
#define ArrayDebug(x,i) cout<<#x<<"["<<i<<"]="<<x[i]<<endl;
#define print(x) out(x);putchar('\n')
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
typedef pair<ll,ll> PLL;
const int INF = 0x3f3f3f3f;
const int maxn = 8e2 + 5;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = asin(1.0) * 2;
const double e = 2.718281828459;
template <class T>
inline bool read(T &ret) {
    char c;
    int sgn;
    if (c = getchar(), c == EOF) {
        return 0;
    }
    while (c != '-' && (c < '0' || c > '9')) {
        c = getchar();
    }
    sgn = (c == '-') ? -1 : 1;
    ret = (c == '-') ? 0 : (c - '0');
    while (c = getchar(), c >= '0' && c <= '9') {
        ret = ret * 10 + (c - '0');
    }
    ret *= sgn;
    return 1;
}
template <class T>
inline void out(T x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) {
        out(x / 10);
    }
    putchar(x % 10 + '0');
}

struct Pos {
	int x, y;
	Pos(int _x = 0, int _y = 0): x(_x), y(_y) {}
};

int T;
int n, m;
int step;
int ans;
int GhostsCnt;
int direction[4][2] = {
	{1, 0},
	{-1, 0},
	{0, 1},
	{0, -1}
};
char plat[maxn][maxn];
Pos Erriyue, GirlFriend, Ghosts[2];
queue<Pos> que[2];

bool judge(Pos x) {
	// 是否是墙壁
	if (plat[x.x][x.y] == 'X') {
		return 0;
	}
	// 是否在地图内
	if (x.x < 0 || x.x >= n || x.y < 0 || x.y >= m) {
		return 0;
	}
	// 曼哈顿距离
	for (int i = 0; i < 2; ++i) {
		if ((abs(Ghosts[i].x - x.x) + abs(Ghosts[i].y - x.y)) <= 2 * step) {
			return 0;
		}
	}
	return 1;
}

bool bfs(int Who, char Mark, char Meet) {
	queue<Pos> keep = que[Who];
	int pace = Who ? 1 : 3;
	for (int i = 0; i < pace; ++i) {
		while (!keep.empty()) {
			Pos temp = keep.front();
			keep.pop();
			que[Who].pop();
			// 鬼先走,判断曼哈顿距离
			if (!judge(temp)) {
				continue;
			}
			for (int j = 0; j < 4; ++j) {
				Pos tmp = temp;
				tmp.x += direction[j][0];
				tmp.y += direction[j][1];
				// 已走过或不可走
				if (!judge(tmp) || plat[tmp.x][tmp.y] == Mark) {
					continue;
				}
				// 另外一个人走过此点
				if (plat[tmp.x][tmp.y] == Meet) {
					return 1;
				}
				plat[tmp.x][tmp.y] = Mark;
				que[Who].push(tmp);
			}
		}
		keep = que[Who];
	}
	return 0;
}

int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
	read(T);
	for (int Case = 1; Case <= T; ++Case) {
		mem(plat, 'X');
		read(n); read(m);
		GhostsCnt = 0;
		for (int i = 0; i < n; ++i) {
			scanf("%s", plat[i]);
			for (int j = 0; j < m; ++j) {
				if (plat[i][j] == 'M') {
					Erriyue.x = i;
					Erriyue.y = j;
				}
				else if (plat[i][j] == 'G') {
					GirlFriend.x = i;
					GirlFriend.y = j;
				}
				else if (plat[i][j] == 'Z') {
					Ghosts[GhostsCnt].x = i;
					Ghosts[GhostsCnt++].y = j;
				}
			}
		}
		while (!que[0].empty()) {
			que[0].pop();
		}
		while (!que[1].empty()) {
			que[1].pop();
		}
		que[0].push(Erriyue);
		que[1].push(GirlFriend);
		step = 0;
		ans = -1;
		while (!que[0].empty() && !que[1].empty()) {
			step++;
			if (bfs(0, 'M', 'G') || bfs(1, 'G', 'M')) {
				ans = step;
				break;
			}
		}
		print(ans);
	}
#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
    system("gedit out.txt");
#endif
    return 0;
}