Nightmare Ⅱ (HDU - 3085,双向 BFS + 模拟)

一.题目链接:

HDU-3085

二.题目大意:

给你一个 n × m 的图,图由以下符号组成.

'.' :空地

'X' :墙

'M' :男

'G' :女

'Z' :鬼

规则:

男、女、鬼 都可以向上下左右四个方向走

每秒男的可以走三步,女的走一步,墙不能走.

每秒每个鬼 会分出子鬼占领距离他单位长度 ≤ 2 的方格(包括墙)

若男 || 女 被鬼抓住,则 GG.

问男、女是否能相遇,若能,则输出所需时间,若不能,则输出 -1.

三.分析:

这题就是双向 BFS  + 模拟.

详见代码吧.

四.代码实现:

#include <set>
#include <map>
#include <ctime>
#include <queue>
#include <cmath>
#include <stack>
#include <vector>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define eps 1e-6
#define pi acos(-1.0)
#define ll long long int
using namespace std;

const int M = (int)8e2;

int n, m;

char mp[M + 5][M + 5];
bool vis[2][M + 5][M + 5];

struct node
{
    int x;
    int y;
};

struct node z[2];
struct node s1, s2;

queue <node> q[2];

int Move[4][2] = {
{0, 1}, {0, -1},
{1, 0}, {-1, 0}
};

bool check(node p, int step)
{
    for(int i = 0; i < 2; ++i)
    {
        if((int)abs(p.x - z[i].x) + (int)abs(p.y - z[i].y) <= step * 2)///曼哈顿距离
            return 0;
    }
    return 1;
}

bool bfs(int id, int step)
{
    struct node p, t;
    int len = q[id].size();
    while((len--) > 0)
    {
        p = q[id].front();
        q[id].pop();
        if(!check(p, step))///因为是鬼先走,所以要考虑此时鬼是否会抓住 id
            continue;
        for(int i = 0; i < 4; ++i)
        {
            t = p;
            t.x += Move[i][0];
            t.y += Move[i][1];
            if(t.x < 0 || t.x >= n || t.y < 0 || t.y >= m || vis[id][t.x][t.y] || mp[t.x][t.y] == 'X' || !check(t, step))
                continue;
            vis[id][t.x][t.y] = 1;
            if(vis[!id][t.x][t.y])
                return 1;
            q[id].push(t);
        }
    }
    return 0;
}

int solve()
{
    for(int i = 0; i < 2; ++i)
    {
        while(!q[i].empty())
            q[i].pop();
    }
    memset(vis, 0, sizeof(vis));
    vis[0][s1.x][s1.y] = vis[1][s2.x][s2.y] = 1;
    q[0].push(s1);
    q[1].push(s2);
    int step = 1;
    while(!q[0].empty() || !q[1].empty())
    {
        for(int i = 0; i < 3; ++i)///男 1 秒走 3 步
        {
            if(bfs(0, step))
                return step;
        }
        if(bfs(1, step))///女 1 秒走 1 步
            return step;
        step++;
    }
    return -1;
}

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d %d", &n, &m);
        int cnt = 0;
        for(int i = 0; i < n; ++i)
        {
            ///getchar();
            scanf("%s", mp[i]);///换成注释中的输入 T 爆了!!!
            for(int j = 0; j < m; ++j)
            {
                ///scanf("%c", &mp[i][j])
                if(mp[i][j] == 'M')
                {
                    s1.x = i;
                    s1.y = j;
                }
                else if(mp[i][j] == 'G')
                {
                    s2.x = i;
                    s2.y = j;
                }
                else if(mp[i][j] == 'Z')
                {
                    z[cnt].x = i;
                    z[cnt].y = j;
                    cnt++;
                }
            }
        }
        int ans = solve();
        printf("%d\n", ans);
    }
    return 0;
}

 

全部评论

相关推荐

03-06 12:44
已编辑
门头沟学院 Java
是个千人厂,没听过名字。1.&nbsp;做一个自我介绍。2.&nbsp;你这个项目和技术栈从哪里学的?有报辅导班嘛[答&nbsp;都是是自己网上学的,学校教的东西没用]3.&nbsp;我看了你放在github上的项目,前端也是你写的嘛[答&nbsp;AI写的,90%精力用于后端开发,前端单纯用于作为后端逻辑的可视化技术验证(骗你的其实后端也是AI写的)]4.&nbsp;好,你觉得这些技术栈研究得最深刻的是哪个[答&nbsp;八股压根没背到后面,昨晚背了MySQL就说MySQL]5.&nbsp;那讲一下MySQL的索引[答&nbsp;从B+树选型一路吟唱到联合索引,索引失效]6.&nbsp;联合索引ABC问题,AB走索引嘛,BC走索引嘛?BAC走索引嘛?A&nbsp;or&nbsp;B&nbsp;走索引嘛[走,不走,走,不走。面试官点头说可以]7.&nbsp;讲一下项目里Redission分布式锁实现8.&nbsp;Watchdog机制具体是怎么工作9.&nbsp;消息队列有考虑过Kafka嘛,怎么选型的10.&nbsp;你这个项目消息队列可能出现什么问题,怎么解决这个问题?[瞎扯没用的,被面试官引导答了视频处理可能产生消息堆积问题,然后开始吟唱]11.&nbsp;文件分片自己写的还是用的什么框架?上传进度的Redis数据结构?上传的视频有多大?小分片大小?12.&nbsp;项目里Redis会话记忆是啥意思?[面试官说不行,没人把这个全放Redis里[生气R]]13.&nbsp;那这和直接查数据库有什么区别[扯了Token成本和解决幻觉问题之类的,给面试官听笑了,我最后也没绷住]14.&nbsp;你平时是怎么使用AI&nbsp;coding的15.&nbsp;算法,给了我一个leedcode链接,一看做过了。然后换了一道三数之和,也做过了。然后面试官说算了,让我讲讲思路吧反问:1.有什么需要提高的地方2.介绍一下部门业务有哪些这个面试官真的感官非常非常好,问问题还疯狂引导,感觉不会也会了。找实习&nbsp;&nbsp;牛客AI配图神器#
查看15道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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