#### HDU 3085 – Nightmare Ⅱ [双向BFS]

http://acm.hdu.edu.cn/showproblem.php?pid=3085

### Problem

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.

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 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.

### Code

```#include <bits/stdc++.h>
using namespace std;

struct Node {
int x, y;
Node(int x = 0, int y = 0):x(x), y(y) {}
};
const int dir = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
const int maxn = 888;

int N, M, cant[maxn][maxn];
char v[maxn][maxn];
Node mm, gg, z1, z2;
queue<Node> qu;
bool vis[maxn][maxn];

void init() {
for(int i = 1; i <= N; ++i) {
for(int j = 1; j <= M; ++j) {
int t1 = (abs(i - z1.x) + abs(j - z1.y) + 1) / 2;
int t2 = (abs(i - z2.x) + abs(j - z2.y) + 1) / 2;
cant[i][j] = min(t1, t2);
}
}
}

inline bool in(int x, int y) {
return x >= 1 && x <= N && y >= 1 && y <= M && v[x][y] != 'X';
}

bool bfs(int x, int step) {
int sz = qu[x].size();
while(sz--) {
Node u = qu[x].front(); qu[x].pop();
if(step >= cant[u.x][u.y])
continue;
for(int i = 0; i < 4; ++i) {
Node v(u.x + dir[i], u.y + dir[i]);
if(in(v.x, v.y) && !vis[v.x][v.y][x] && step < cant[v.x][v.y]) {
if(vis[v.x][v.y][!x])
return true;
vis[v.x][v.y][x] = true;
qu[x].push(v);
}
}
}
return false;
}

int solve() {
while(!qu.empty()) qu.pop();
while(!qu.empty()) qu.pop();
qu.push(mm), qu.push(gg);
memset(vis, false, sizeof(vis));
vis[mm.x][mm.y] = vis[gg.x][gg.y] = true;
int step = 0;
while(!qu.empty() && !qu.empty()) {
++step;
if(bfs(0, step) || bfs(0, step) || bfs(0, step) || bfs(1, step))
return step;
}
return -1;
}

int main() {
int T; scanf("%d", &T);
while(T--) {
scanf("%d%d", &N, &M);
mm = gg = z1 = z2 = Node(0, 0);
for(int i = 1; i <= N; ++i) {
scanf("%s", v[i] + 1);
for(int j = 1; j <= M; ++j) {
if(v[i][j] == 'M') mm = Node(i, j);
else if(v[i][j] == 'G') gg = Node(i, j);
else if(v[i][j] == 'Z') {
if(!z1.x) z1 = Node(i, j);
else z2 = Node(i, j);
}
}
}
init();
printf("%d\n", solve());
}
return 0;
}
```