题解:

简单bfs

显然不能到负数

也不能超过k两倍

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=200005;
int b[N],vis[N],f[N];
int n,k;
int main()
{
    scanf("%d%d",&n,&k);
    int l=0,r=1;
    b[0]=n;
    vis[n]=1;
    while (l<r)
     {
         int now=b[l++];
         if (now-1>=0&&!vis[now-1])
          {
              vis[now-1]=1;
              b[r++]=now-1;
              f[now-1]=f[now]+1;
         }
         if (now+1<=2*k&&!vis[now+1])
          {
              vis[now+1]=1;
              b[r++]=now+1;
              f[now+1]=f[now]+1;
         }
         if (now*2<=2*k&&!vis[now*2])
          {
              vis[now*2]=1;
              b[r++]=now*2;
              f[now*2]=f[now]+1;
         }                  
     }
    printf("%d",f[k]); 
}

 

posted on 2018-02-22 15:52  宣毅鸣  阅读(86)  评论(0编辑  收藏  举报