HDU3085 Nightmare Ⅱ 双向bfs

Nightmare Ⅱ

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

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


题意两个鬼 向四周扩散 墙也可以 所到之地都变鬼   每秒两步 

男人一秒走三步  女人一 步

问男女 能   在鬼抓 到之前相遇吗(一个被抓就GG) 

如果能 就输出秒数


//搜索的关键是  鬼走一步 人走一步 

#include
#include
#include
#include
using namespace std;
int dir[4][2]= {1,0,0,1,-1,0,0,-1};
char ap[800][800];
struct node
{
    int x,y;
} ft,et;
int n,m;
int nrx,nry,nvx,nvy,gx[3],gy[3],step;
int pd(node a)
{
    if(a.x<0||a.x>=n||a.y<0||a.y>=m||ap[a.x][a.y]=='X')
        return 1;
    return 0;
}
int check(node a)
{
    for(int i=0; i<2; i++)
        if(abs(a.x-gx[i])+abs(a.y-gy[i])<=2*step)    // 经过step次 是否这个位置会被鬼占领
            return 1;
    return 0;


}
queueq[2],qt;
int bfs(int k,int num,char a,char b)
{
    int i,j;
    qt=q[k];    //   关键
    for(i=0; i


你可能感兴趣的:(搜索,ACM荣耀之路)