HDU 3085 – Nightmare Ⅱ [双向BFS]

Link

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.

Mean

在N*M的网格中,有两个魔鬼,每次可以走两步分裂。有一个人和他女朋友,他每次可以走三步,女朋友每次可以走一步。魔鬼先走人后走,碰到魔鬼就会死亡,两人见面就生还并输出步数。

Analysis

先算出每个格子被魔鬼占领时的时间,然后用两个队列分别bfs,知道碰面为止。具体看代码吧。

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[4][2] = {{-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[2];
bool vis[maxn][maxn][2];

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][0], u.y + dir[i][1]);
            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[0].empty()) qu[0].pop();
    while(!qu[1].empty()) qu[1].pop();
    qu[0].push(mm), qu[1].push(gg);
    memset(vis, false, sizeof(vis));
    vis[mm.x][mm.y][0] = vis[gg.x][gg.y][1] = true;
    int step = 0;
    while(!qu[1].empty() && !qu[2].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;
}

欢迎留言