计算疫情扩散时间

一.题目

二.歧义

此题没有歧义

三.解题思路

这道题剔除一些无关简要的代码,就是一个循环就搞定了。

四.代码

1.主函数

先上主流程

  • 因为题目中说了,输入的数据是以“,”分隔,所以上来就是分割
  • 对生产出来的数据进行校验
  • 根据用户输入的数据 初始化地图信息
  • 校验是否全部感染 或者 全部没有被感染
  • 计算感染时间
void main()
{
    std::string str;
    std::string reg = ",";
    std::getline(std::cin, str);
    std::vector<int> nums = SplitStringToDigit(str, reg);

    if (nums.empty())
    {
        std::cout << "-1" << std::endl;
        return;
    }
    int n = std::sqrt(nums.size());
    //根据用户输入的数据 初始化地图信息
    InitMap(nums, n);
    //校验是否全部感染 或者 全部没有被感染
    if (IsAllInfect(n) || IsAllNoInfect(n))
    {
        std::cout << "-1" << std::endl;
        return;
    }
    std::cout << CalcInfectDays(n) << std::endl;

}

2.分割

因为题目中说了,输入的数据是以“,”分隔,所以必然还是老代码

//分隔字符串并将其转换为数字
std::vector<int> SplitStringToDigit(std::string& str, const std::string& reg)
{
    std::vector<int> result;
    std::string::size_type posLast = 0;
    std::string::size_type posCurrent = str.find(reg);

    while (posCurrent != std::string::npos)
    {
        result.push_back(str.substr(posLast, posCurrent - posLast).c_str()[0] - '0');
        posLast = posCurrent + reg.size();
        posCurrent = str.find(reg, posLast);
    }

    if (posLast < str.length())
    {
        result.push_back(str.substr(posLast).c_str()[0] - '0');
    }
    return result;
}

3.初始化地图

//根据输入的数据初始化map
void InitMap(const std::vector<int>& nums,int n)
{
    for (int i = 0; i < nums.size(); i++)
    {
        int row = i / n;
        int cell = i % n;
        map[row][cell] = nums[i];
    }
}

4.核心代码

//获取所有被感染的节点
std::vector<std::pair<int, int>> GetInfectNodes(int n)
{
    //获取所有被感染的节点
    std::vector<std::pair<int, int>> infectNodes;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            //0 没有被感染, 1被感染
            if (map[i][j] == 1)
            {
                infectNodes.push_back(std::make_pair(i, j));
            }
        }
    }
    return infectNodes;
}

int CalcInfectDays(int n)
{
    std::vector<std::pair<int, int>> infectNodes = GetInfectNodes(n);
    int days = 0;
    while (!IsAllInfect(n))
    {
        //病毒可以一传百,百传千,这里一定要重新获取病毒源
        infectNodes = GetInfectNodes(n);
        for (auto& node : infectNodes)
        {
            for (auto& dir : directions)
            {
                int row = node.first + dir[0];
                int cell = node.second + dir[1];
                //如果超越n的边界了,就不处理
                if (row < 0 || row >= n || cell < 0 || cell >= n) 
                    continue;
                map[row][cell] = 1;
            }
        }
        days++;
    }
    return days;
}

五.全部代码

#include <iostream>
#include <vector>
#include <cmath>
#include <string>


//分隔字符串并将其转换为数字
std::vector<int> SplitStringToDigit(std::string& str, const std::string& reg)
{
    std::vector<int> result;
    std::string::size_type posLast = 0;
    std::string::size_type posCurrent = str.find(reg);

    while (posCurrent != std::string::npos)
    {
        result.push_back(str.substr(posLast, posCurrent - posLast).c_str()[0] - '0');
        posLast = posCurrent + reg.size();
        posCurrent = str.find(reg, posLast);
    }

    if (posLast < str.length())
    {
        result.push_back(str.substr(posLast).c_str()[0] - '0');
    }
    return result;
}



//由于题目限定了地图的最大范围,这里设定一个最大二维数组
int map[200][200];
int directions[4][2] = { {1,0},{0,1},{-1,0},{0,-1} };

//根据输入的数据初始化map
void InitMap(const std::vector<int>& nums,int n)
{
    for (int i = 0; i < nums.size(); i++)
    {
        int row = i / n;
        int cell = i % n;
        map[row][cell] = nums[i];
    }
}

//校验地图是否全部被感染了
bool IsAllInfect(int n)
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            //0 没有被感染, 1被感染
            if (map[i][j] == 0) return false;
        }
    }
    return true;
}

//校验地图是否全部没有被感染
bool IsAllNoInfect(int n)
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            //0 没有被感染, 1被感染
            if (map[i][j] == 1) return false;
        }
    }
    return true;
}

//获取所有被感染的节点
std::vector<std::pair<int, int>> GetInfectNodes(int n)
{
    //获取所有被感染的节点
    std::vector<std::pair<int, int>> infectNodes;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            //0 没有被感染, 1被感染
            if (map[i][j] == 1)
            {
                infectNodes.push_back(std::make_pair(i, j));
            }
        }
    }
    return infectNodes;
}

int CalcInfectDays(int n)
{
    std::vector<std::pair<int, int>> infectNodes = GetInfectNodes(n);
    int days = 0;
    while (!IsAllInfect(n))
    {
        //病毒可以一传百,百传千,这里一定要重新获取病毒源
        infectNodes = GetInfectNodes(n);
        for (auto& node : infectNodes)
        {
            for (auto& dir : directions)
            {
                int row = node.first + dir[0];
                int cell = node.second + dir[1];
                //如果超越n的边界了,就不处理
                if (row < 0 || row >= n || cell < 0 || cell >= n) 
                    continue;
                map[row][cell] = 1;
            }
        }
        days++;
    }
    return days;
}





void DebugTest()
{
    std::vector<int> nums = { 1,0,1,0,0,0,1,0,1 };
    int n = std::sqrt(nums.size());
    InitMap(nums, n);
    if (IsAllInfect(n) || IsAllNoInfect(n))
    {
        std::cout << "-1" << std::endl;
        return;
    }
    std::cout << CalcInfectDays(n) << std::endl;
}



void main()
{
    std::string str;
    std::string reg = ",";
    std::getline(std::cin, str);
    std::vector<int> nums = SplitStringToDigit(str, reg);

    if (nums.empty())
    {
        std::cout << "-1" << std::endl;
        return;
    }
    int n = std::sqrt(nums.size());
    //根据用户输入的数据 初始化地图信息
    InitMap(nums, n);
    //校验是否全部感染 或者 全部没有被感染
    if (IsAllInfect(n) || IsAllNoInfect(n))
    {
        std::cout << "-1" << std::endl;
        return;
    }
    std::cout << CalcInfectDays(n) << std::endl;

}

华为OD2024 E 文章被收录于专栏

实时更新华为2024 E卷答案

全部评论

相关推荐

不愿透露姓名的神秘牛友
04-30 17:45
本人简历上&nbsp;1&nbsp;个&nbsp;RAG&nbsp;项目&nbsp;+&nbsp;1&nbsp;个&nbsp;Agent&nbsp;demo;这次面的是AI岗一面前我以为:背完八股&nbsp;+&nbsp;把项目讲清楚,应该能稳过。0-5&nbsp;min:自我介绍&nbsp;+&nbsp;项目背景-&nbsp;顺利。讲清楚了我的&nbsp;RAG&nbsp;是给法律咨询场景做的,痛点是大模型不懂行业术语。5-20&nbsp;min:项目深挖(开始崩)-&nbsp;Q1:你的法律文档总共多少?切了多少个&nbsp;chunk?-&nbsp;我:约&nbsp;500&nbsp;份&nbsp;PDF,5&nbsp;万个&nbsp;chunk-&nbsp;Q2:500&nbsp;份&nbsp;PDF&nbsp;加起来才&nbsp;5&nbsp;万&nbsp;chunk?平均每份&nbsp;100&nbsp;个&nbsp;chunk,你切片粒度是多少?-&nbsp;我:512&nbsp;token-&nbsp;Q3:法律文档里"第三条第二款"和"第三条之二"是不同含义,你的切片会不会把它切散?-&nbsp;我:(沉默&nbsp;5&nbsp;秒)……应该会-&nbsp;Q4:那你怎么解决?-&nbsp;我:我可以加一个&nbsp;metadata……(开始编)❌&nbsp;第一次崩:切片粒度没考虑业务语义。20-35&nbsp;min:评测体系(继续崩)-&nbsp;Q:你怎么知道你的&nbsp;RAG&nbsp;有效?-&nbsp;我:我用&nbsp;Recall@5……-&nbsp;Q:评测集多少条?怎么构造的?-&nbsp;我:100&nbsp;条,我手工标注的-&nbsp;Q:100&nbsp;条够吗?分布怎么样?-&nbsp;我:分布……我没分-&nbsp;Q:那你的&nbsp;Recall@5&nbsp;是&nbsp;0.81,你怎么知道这个数字是好是坏?baseline&nbsp;是什么?-&nbsp;我:(沉默&nbsp;10&nbsp;秒)❌&nbsp;第二次崩:没有&nbsp;baseline,没分布分析,纯靠"看起来还行"。35-55&nbsp;min:Agent&nbsp;部分(彻底崩)-&nbsp;Q:你的&nbsp;Agent&nbsp;demo&nbsp;用了几个工具?-&nbsp;我:3&nbsp;个,搜索、计算器、文档查询-&nbsp;Q:当用户问一个问题,你的&nbsp;Agent&nbsp;怎么决定调哪个工具?-&nbsp;我:用&nbsp;ReAct,让模型自己决定-&nbsp;Q:模型决策错了怎么办?-&nbsp;我:我加了个&nbsp;reflection……-&nbsp;Q:reflection&nbsp;失败&nbsp;3&nbsp;次后怎么处理?-&nbsp;我:(沉默&nbsp;15&nbsp;秒)……我没想过❌&nbsp;第三次崩:异常路径完全没设计。55-65&nbsp;min:业务理解&nbsp;+&nbsp;反问-&nbsp;Q:你觉得字节做&nbsp;AI&nbsp;应用最大的瓶颈是什么?-&nbsp;我:算力?数据?-&nbsp;Q:你看过哪些字节最近发的&nbsp;AI&nbsp;产品?-&nbsp;我:豆包、扣子……-&nbsp;Q:扣子是&nbsp;Agent&nbsp;平台还是工作流平台?-&nbsp;我:(再次沉默)❌&nbsp;第四次崩:对面试公司业务一无所知。
牛客41664080...:切片粒度问得好
面试官拷打AI项目都会问...
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

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