题目分析:很水的 BFS ,注意边界
#include<iostream>
#include<cstdio>
#include<memory.h>
#include<queue>
using namespace std;
const int maxn=2100000;
int a[maxn],f[maxn];
int main()
{
int n,k;
while(scanf("%d %d",&n,&k)!=EOF)
{
int i,j,cur,temp;
memset(a,0,sizeof(a));
queue<int>q;
q.push(n);
a[n]=1, f[n]=0;
while(!q.empty())
{
cur=q.front();
q.pop();
if(cur==k)
break;
temp=cur-1;
//printf("%d*\t",cur);
if(temp>=0 && a[temp]==0)
{
q.push(temp);
a[temp]=1;
f[temp]=f[cur]+1;
}
temp=cur+1;
if(a[temp]==0 && temp<=k)
{
q.push(temp);
a[temp]=1;
f[temp]=f[cur]+1;
}
temp=cur*2;
if(a[temp]==0&&temp<=2*k)//注意下是2*k
{ //不是k
q.push(temp);
a[temp]=1;
f[temp]=f[cur]+1;
}
}
printf("%d\n",f[cur]);
}
return 0;
}