【刷爆LeetCode】六月算法集训(17)广度优先搜索

这篇博客分享了英雄算法联盟的六月集训专题,包括三道中等难度的算法题目的详细解析和C++代码实现。分别是2059.转化数字的最小运算数、690.员工的重要性以及672.灯泡开关II。每道题目从描述、解题思路到代码演示都有详尽的阐述,适合进阶算法学习者参考。
摘要由CSDN通过智能技术生成

题目来源于知识星球—英雄算法联盟,六月算法集训专题

前言

跟随英雄算法联盟博主—英雄哪里出来,每天完成相应的算法练习,一个月后,必定会有所成长!


一、2059.转化数字的最小运算数(中等)

1.题目描述

https://img-blog.csdnimg.cn/9228089a7fce408c9bef9d20774f271b.jpeg =500x

2.解题思路

看题解敲得,纯广搜模板,每次遍历数组,入队,扩展状态。

3.代码演示(C++)

#define maxn 1010

class Solution 
{
public:
    int minimumOperations(vector<int>& nums, int start, int goal) 
    {
        queue<int> q;
        int step[maxn];
        int x, nextx, i;
        memset(step, -1, sizeof(step));
        q.push(start);
        step[start] = 0;
        while(!q.empty()) 
        {
            x = q.front();
            q.pop();
            for(i = 0; i < nums.size(); ++i) 
            {
                nextx = x + nums[i];
                if(nextx == goal) 
                {
                    return step[x] + 1;
                }
                if(nextx <= 1000 && nextx >= 0) 
                {
                    if(step[nextx] == -1) 
                    {
                        step[nextx] = step[x] + 1;
                        q.push(nextx);
                    }
                }
                nextx = x - nums[i];
                if(nextx == goal) 
                {
                    return step[x] + 1;
                }
                if(nextx <= 1000 && nextx >= 0) 
                {
                    if(step[nextx] == -1) 
                    {
                        step[nextx] = step[x] + 1;
                        q.push(nextx);
                    }
                }
                nextx = ( x ^ nums[i] ); 
                if(nextx == goal) 
                {
                    return step[x] + 1;
                }
                if(nextx <= 1000 && nextx >= 0) 
                {
                    if(step[nextx] == -1) 
                    {
                        step[nextx] = step[x] + 1;
                        q.push(nextx);
                    }
                }
            }
        }
        return -1;
    }
};

4.题目链接

题目传送门


二、690.员工的重要性(中等)

1.题目描述

https://img-blog.csdnimg.cn/2d4646142b14485bbfd589dec0946081.jpeg =500x

2.解题思路

下次再战!

3.代码演示(C++)


4.题目链接

题目传送门


三、672.灯泡开关 II(中等)

1.题目描述

https://img-blog.csdnimg.cn/43544a780b9f4ca1b4642362deeb23da.jpeg =500x

2.解题思路

下次再战!

3.代码演示(C++)


4.题目链接

题目传送门


总结

每天跟随英雄哥学习相关的算法,一个月会收获很多,如果你想了解更多关于知识星球的内容,欢迎联系我!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值