[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;
}
全部评论

相关推荐

12-18 18:50
已编辑
门头沟学院 golang
牛客33637108...:重点是要事已密成,在没有进入这家公司之前,不要有任何的泄露信息,我之前跟你一样,面了一家光伏设备厂,底薪7500加上出差补贴大概有13,000左右,已经给了口头offer了,甚至要了我的在校成绩的所有信息,还向我要了三方的网签二维码,到后面还是毁约了,我干过最愚蠢的事情就是向同学透露要签三方的事,之后的失败只会让他们幸灾乐祸,这是即将结束的大学生活给我的最后一课,不要相信任何的口头三方,该面的就去面,甚至签了三方也有毁约的可能,就像我现在签了三方还在外面实习呢,春招还是要继续参加的,不能停止面试,不然到后面毁三方的时候,重新捡起的面试很麻烦的,这是我一点点小小的见解。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务