简单bfs+pair的用法

链接:https://www.nowcoder.com/acm/contest/106/J
来源:牛客网


It’s universally acknowledged that there’re innumerable trees in the campus of HUST.


And there are many different types of trees in HUST, each of which has a number represent its type. The doctors of biology in HUST find 4 different ways to change the tree’s type x into a new type y:
1.    y=x+1
2.    y=x-1
3.    y=x+f(x)
4.    y=x-f(x)
The function f(x) is defined as the number of 1 in x in binary representation. For example, f(1)=1, f(2)=1, f(3)=2, f(10)=2.
Now the doctors are given a tree of the type A. The doctors want to change its type into B. Because each step will cost a huge amount of money, you need to help them figure out the minimum steps to change the type of the tree into B. 


Remember the type number should always be a natural number (0 included).
输入描述:
One line with two integers A and B, the init type and the target type.
输出描述:

You need to print a integer representing the minimum steps.

思路:利用pair进行边搜边记录步数,bfs

代码:

#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
const int maxn=1e6+5;
typedef pair<int ,int >PII;
int x,y;
int vis[maxn];
int f[maxn];
void init()
{
    int lala;
    int num;
    for(int i=1;i<=1000000;i++)
    {
        num=0;
        lala=i;
        while(lala!=0)
        {
            if(lala%2==1)
                num++;
            lala=lala/2;
        }
        f[i]=num;
    }
}
/*
int f(int u)
{
    int ret = 0;
    while (u)
        ret += (u & 1), u >>= 1;
    return ret;
}
*/
int main()
{
    init();
    while(scanf("%d%d",&x,&y)!=EOF)
    {
        queue<PII>q;
        q.push(PII(x,0));
       // for(int i=0;i<=max(x,y);i++)
        //{
          //  vis[i]=0;
        //}
        memset(vis,0,sizeof(vis));
        vis[x]=1;
        while(!q.empty())
        {
            PII p=q.front();
            q.pop();
            int u=p.x;
            int v=p.y;
            int uu;
            if(u==y)
            {
                cout<<v<<endl;
                break;
            }
            uu=u+1;
            if(!vis[uu])
            {
                vis[uu]=1;
                q.push(PII(uu,v+1));
            }
            uu=u-1;
            if(!vis[uu])
            {
                vis[uu]=1;
                q.push(PII(uu,v+1));
            }
            uu=u+f[u];
            if(!vis[uu])
            {
                vis[uu]=1;
                q.push(PII(uu,v+1));
            }
            uu=u-f[u];
            if(!vis[uu])
            {
                vis[uu]=1;
                q.push(PII(uu,v+1));
            }
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 BFS 算法解决子集和问题的 C++ 代码示例: ```c++ #include <iostream> #include <queue> #include <vector> using namespace std; bool subset_sum_bfs(vector<int>& nums, int target) { int n = nums.size(); queue<pair<int, int>> q; // (index, current_sum) q.push({0, 0}); while (!q.empty()) { auto [index, current_sum] = q.front(); q.pop(); if (current_sum == target) { return true; } if (index == n) { continue; } q.push({index + 1, current_sum + nums[index]}); q.push({index + 1, current_sum}); } return false; } // 示例用法 int main() { vector<int> nums = {3, 34, 4, 12, 5, 2}; int target = 9; cout << (subset_sum_bfs(nums, target) ? "True" : "False") << endl; // 输出 True return 0; } ``` 这段代码与 Python 版本的代码非常类似,只是语法不同。在 C++ 中,我们使用了 `std::queue` 来保存搜索过程中的状态,每个状态使用一个 `std::pair` 来表示,第一个元素表示当前搜索到的数的下标,第二个元素表示当前已经选择的数的和。初始状态为 `(0, 0)`,表示还没有选择任何数,当前和为 0。然后我们每次从队列中取出一个状态,根据这个状态可以分为两种情况: 1. 将当前数加入当前和中,得到一个新的状态 `{index + 1, current_sum + nums[index]}`,将其加入队列。 2. 不将当前数加入当前和中,得到一个新的状态 `{index + 1, current_sum}`,将其加入队列。 这样一直搜索下去,直到队列为空或者找到了一个符合条件的状态,即当前和等于目标值。如果搜索完整个数组都没有找到符合条件的状态,则返回 False。 需要注意的是,这种方法的时间复杂度为 $O(2^n)$,其中 $n$ 为数组的长度。因此,在实际应用中,当 $n$ 比较大时,可能需要采用其他更高效的算法来解决子集和问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值