题目链接:https://ac.nowcoder.com/acm/contest/984/L
题目大意:

思路:开始想的dfs发现复杂度很高,并且会重复访问一个位置,这种最少步数的适合bfs,O(n)的复杂度。访问的点不能再访问。

#include <bits/stdc++.h>
#define LL long long
using namespace std;

int vis[200010];

int OK(pair<int , int> now)
{
    if(now.first<0||now.first>200000||vis[now.first])
    {
        return 0;
    }
    return 1;
}

queue<pair<int , int> > q;
int dfs(int n, int k)
{
    q.push({n, 0});
    vis[n]=1;

    while(!q.empty())
    {
        pair<int , int> now=q.front();
        q.pop();
        if(now.first==k)
        {
            return now.second;
        }

        pair<int , int> t;

        t.first=now.first+1;
        t.second=now.second+1;
        if(OK(t))
        {
            vis[t.first]=1;
            q.push(t);
        }

        t.first=now.first-1;
        if(OK(t))
        {
            vis[t.first]=1;
            q.push(t);
        }

        t.first=now.first*2;
        if(OK(t))
        {
            vis[t.first]=1;
            q.push(t);
        }
    }
}

int main()
{
    int n, k;
    scanf("%d%d",&n,&k);
    cout<<dfs(n ,k)<<endl;

    return 0;
}