[HDU3085] 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<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
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
---------------------------

题意:判断男孩女孩能否在鬼抓到其中一人前相遇

思路:双向bfs,鬼的分裂可以用'当前时间乘以2'与'人和鬼之间的曼哈顿距离'来判断,当发现某个点被两个人都走过时即找到了最短相遇点,输出即可

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <bitset>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define rg register
#define LL long long
#define __space putchar(' ')
#define __endl putchar('\n')
template <typename qwq> inline void read(qwq & x)
{
    x = 0;
    rg int f = 1;
    rg char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-') f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
    {
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }
    x *= f;
}
template <typename qaq> inline void print(qaq x)
{
    if (x < 0)
    {
        putchar('-');
        x = -x;
    }
    if (x > 9) print(x / 10);
    putchar(x % 10 + '0');
}
int dx[] = {0,0,0,1,-1};
int dy[] = {0,1,-1,0,0};
char Map[805][805];
int t,n,m;
bool bvis[805][805],gvis[805][805];
struct node
{
    int x,y;
}boy,girl,zom1,zom2;
queue<node> b,g;
inline void prework()//输入,重置,找到男孩女孩和鬼 
{
    rg bool flag = false;
    for (rg int i = 1;i <= n;++i) scanf("%s",Map[i] + 1);
    for (rg int i = 1;i <= n;++i)
    {
        for (rg int j = 1;j <= m;++j)
        {
            bvis[i][j] = gvis[i][j] = false;
            if (Map[i][j] == 'M') boy.x = i,boy.y = j;
            if (Map[i][j] == 'G') girl.x = i,girl.y = j;
            if (Map[i][j] == 'Z' && !flag) zom1.x = i,zom1.y = j,flag = true;
            if (Map[i][j] == 'Z' && flag) zom2.x = i,zom2.y = j; 
        }
    }
    while (!b.empty()) b.pop();
    while (!g.empty()) g.pop();
}
inline bool judge(int x,int y)//边界判断 
{
    if (x < 1 || x > n || y < 1 || y > m) return false;
    if (Map[x][y] == 'X') return false;
    return true;
}
inline bool Manhattan (int x,int y,int tm)//曼哈顿距离判断 
{
    if (abs(x - zom1.x) + abs(y - zom1.y) <= 2 * tm) return false;
    if (abs(x - zom2.x) + abs(y - zom2.y) <= 2 * tm) return false;
    return true;
}
inline bool boy_friend_search()
{
    rg int tm = 0,size;
    b.push(boy),g.push(girl);
//  bvis[boy.x][boy.y] = gvis[girl.x][girl.y] = true;
    while (!b.empty() || !g.empty())
    {
        ++tm;
        rg int ntm = 3;
        while (ntm--)//男孩走3步 
        {
            size = b.size();
            while (size--)
            {
                rg node nb = b.front();
                b.pop();
                rg int bx = nb.x,by = nb.y;
                if (!Manhattan(bx,by,tm)) continue;//鬼比人先走,所以要先判断是否撞鬼! 
                for (rg int i = 1;i <= 4;++i)
                {
                    rg int nx = bx + dx[i];
                    rg int ny = by + dy[i];
                    if (!judge(nx,ny)) continue;
                    if (!Manhattan(nx,ny,tm)) continue;
                    if (bvis[nx][ny]) continue;//走过了就不要走了,不然MLE(别问我怎么知道的 
                    bvis[nx][ny] = true;
                    if (gvis[nx][ny])
                    {
                        print(tm);
                        return true;
                    }
                    b.push((node) {nx,ny});
                }
            }
        }
        size = g.size();
        while (size--)//女孩走一步,同上 
        {
            rg node ng = g.front();
            g.pop();
            rg int gx = ng.x,gy = ng.y;
            if (!Manhattan(gx,gy,tm)) continue;
            for (rg int i = 1;i <= 4;++i)
            {
                rg int nx = gx + dx[i];
                rg int ny = gy + dy[i];
                if (!judge(nx,ny)) continue;
                if (!Manhattan(nx,ny,tm)) continue;
                if (gvis[nx][ny]) continue;
                gvis[nx][ny] = true;
                if (bvis[nx][ny])
                {
                    print(tm);
                    return true;
                }
                g.push((node) {nx,ny});
            }
        }
    }
    return false;
}
int main()
{
    read(t);
    while (t--)
    {
        read(n),read(m);
        prework();
        if (!boy_friend_search()) print(-1);
        __endl;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Here-is-SG/p/10532953.html