风雨同行 发表于 2024-8-1 13:06:58

bfs宽搜

Problem - B - Codeforces

https://i-blog.csdnimg.cn/direct/6a90ff6542c343fa9cdf99483bf3c622.png
#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef pair<int,int> PII;

const int N=1e4+10;
int q, hh,tt;
int d;
int n,m;

int bfs(int u)
{
        memset(d,-1,sizeof d);
        q=u;
        d=0;
        while(hh<=tt){
                int t=q;
                if(t==m) return d;
                int j=t-1;
                if(j>=0 && j<=10000 && d==-1){
                        q[++tt]=j;
                        d=d+1;
                }
                j=t*2;
                if(j>=0 && j<=10000 && d==-1){
                        q[++tt]=j;
                        d=d+1;
                }
        }
}

void solve()
{
        cin>>n>>m;
        int cnt=0;
        if(n>=m) cnt=n-m;
        else {
                cnt=bfs(n);
        }
        cout<<cnt<<'\n';
       
}

int main()
{
        int T;
//        cin>>T;
        T=1;
        while(T--){
                solve();
        }
        return 0;
}

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: bfs宽搜