Nightmare Ⅱ@HDU3085

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

 

双向DFS,判断使用Ghost时使用曼哈顿距离(就是abs(x)+abs(y)),一直想着色,钻牛角尖了。。。

 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define MAXN 806
#define MOD 1000000009
#define INF 0x7ffffff
#define ABS(x) -(abs(x)%MOD)
#define JUDGE(x) (x < 0 ? ABS(x) : x%MOD)

using namespace std;

int mx,my,gx,gy,zx1,zy1,zx2,zy2; //四个关键坐标
char edge[MAXN][MAXN];
int vis[2][MAXN][MAXN]; 
int dir[4][2] = {0,1,1,0,0,-1,-1,0};
int n,m;
int step;
struct Node{
    int x,y;
};
queue que[2];

void init(){
    memset(vis,0,sizeof(vis));
    zx1 = zy1 = zx2 = zy2 = -1;
    while(!que[0].empty()) que[0].pop();
    while(!que[1].empty()) que[1].pop();
    step = 0;
}

bool is_ok(int tx,int ty,int x){
    if(tx < 0 || tx >= n || ty < 0 || ty >= m || vis[x][tx][ty] || edge[tx][ty] == 'X') return false;
    if(abs(zx1-tx) + abs(zy1-ty) <= 2*step) return false; //曼哈顿距离
    if(abs(zx2-tx) + abs(zy2-ty) <= 2*step) return false;
    return true;
}

bool DFS(int x){
    int sum = que[x].size();
    Node node,tmp;
    while(sum--){
        node = que[x].front();
        que[x].pop();
        //题目要求假设Ghost先走,即M或G站在此处时Ghost到达了,也是不合适的,所以多加一步判断
        if(abs(node.x-zx1) + abs(node.y-zy1) <= 2*step) continue;
        if(abs(node.x-zx2) + abs(node.y-zy2) <= 2*step) continue;
        for(int i=0;i<4;++i){
            int tx = node.x + dir[i][0];
            int ty = node.y + dir[i][1];
            if(!is_ok(tx,ty,x)) continue;
            if(vis[x^1][tx][ty] == 1) return true;
            vis[x][tx][ty] = 1;
            tmp.x = tx;
            tmp.y = ty;
            que[x].push(tmp);
        }
    }
    return false;
}

int solve(){
    vis[0][mx][my] = vis[1][gx][gy] = 1;
    Node node;
    node.x = mx,node.y = my;
    que[0].push(node);
    node.x = gx,node.y = gy;
    que[1].push(node);

    while(!que[0].empty() || !que[1].empty()){
        ++step;
        if(DFS(0)){
            return step;
        }
        if(DFS(0)){
            return step;
        }
        if(DFS(0)){
            return step;
        }        
        if(DFS(1)){
            return step;
        }
    }
    return -1;
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        init();
        scanf("%d%d",&n,&m);
        for(int i=0;i

 

你可能感兴趣的:(DFS)