HDU Problem - 3085 Nightmare Ⅱ(双向BFS)

题目链接

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

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
AC
  • 对鬼魂的处理:每次先计算鬼经过当前时间能否到达M和G的位置(鬼魂先移动),然后判断当M和G移动之后能否到达(移动之后位置的合法性)
  • 对于M和G:记录他们所走的位置,因为虽然可能不是同时到达,但是如果他们都曾经到达一个地方(首先这个地方已经安全),那么其中一个人完全可以等着另一个人
  • 用两个BFS求最短距离
#include <bits/stdc++.h>
using namespace std;
int n, m;
char a[802][802];
bool vis[2][802][802];
vector<pair<int, int> > z; 
queue<pair<int, int> > q[2];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

bool judge(int x, int y, int time) {
    if (x < 0 || y < 0 || x >= n || y >= m || a[x][y] == 'X')
        return false;
    if (fabs(x - z[0].first) + fabs(y - z[0].second) <= time * 2)
        return false;
    if (fabs(x - z[1].first) + fabs(y - z[1].second) <= time * 2)
        return false;
    return true;
}
bool bfs(int t, int time) {
    int len = q[t].size();
    // 当前所有状态移动一位 
    while (len--) {
        int x = q[t].front().first;
        int y = q[t].front().second;
        q[t].pop();
        // 鬼魂先走 
        if (!judge(x, y, time)) continue;
        int xx, yy;
        for (int i = 0; i < 4; ++i) {
            xx = x + dx[i];
            yy = y + dy[i];
            if (!judge(xx, yy, time) || vis[t][xx][yy]) continue;
            if (vis[(t ^ 1)][xx][yy])   return true;
            if (vis[t][xx][yy]) continue;
            vis[t][xx][yy] = true;
            q[t].push(make_pair(xx, yy));
        }
    }
    return false;
}


int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif
    int t;
    scanf("%d", &t);
    while (t--) {
        memset(vis, false, sizeof(vis));
        scanf("%d%d", &n, &m);
        for (int i = 0; i < n; ++i) {
            scanf("%s", a[i]);
            for (int j = 0; j < m; ++j) {
                if (a[i][j] == 'M')
                    q[0].push(make_pair(i, j)), vis[0][i][j] = true;
                if (a[i][j] == 'G')
                    q[1].push(make_pair(i, j)), vis[1][i][j] = true;
                if (a[i][j] == 'Z')
                    z.push_back(make_pair(i, j));
            }
        }
        int ans = 1;
        while (1) {
            // M走三步 
            if (bfs(0, ans))    break;
            if (bfs(0, ans))    break;
            if (bfs(0, ans))    break;
            // G走一步 
            if (bfs(1, ans))    break;
            if (q[0].empty() && q[1].empty()) {
                ans = -1;
                break;
            } 
            ++ans;
        }
        printf("%d\n", ans);
        while (!q[1].empty()) {
            q[1].pop();
        }
        while (!q[0].empty()) {
            q[0].pop();
        }
        z.clear();
    }
    return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值