1 条题解

  • 1
    @ 2024-2-8 11:39:43

    可利用宽搜解决:

    #include<bits/stdc++.h>
    using namespace std;
    int a, b, setp[2000001];
    bool v[2000001];
    queue<int> q;
    void push(int x, int c)
    {
        if (!v[x])
        {
            q.push(x);
            v[x] = 1;
            setp[x] = c;
        }
    }
    int main(){
        cin >> a >> b;
        push(a, 0);
        while (!q.empty())
        {
            int x = q.front(), c = setp[x];
            q.pop();
            if (x > 0)
                push(x - 1, c + 1);
            if (x < 1000000)
                push(x + 1, c + 1);
            if (x < 1000000)
                push(x * 2, c + 1);
        }
        cout << setp[b] << endl;
    	return 0;
    }
    
    • 1

    信息

    ID
    1107
    时间
    1000ms
    内存
    64MiB
    难度
    6
    标签
    递交数
    45
    已通过
    14
    上传者