POJ_3278
这个是个比较简单的BFS的题目。
#include<stdio.h>
#include<string.h>
int dis[200010],q[200010];
int main()
{
int i,j,N,K,front,rear,n,newn;
while(scanf("%d%d",&N,&K)==2)
{
memset(dis,-1,sizeof(dis));
dis[N]=0;
front=rear=0;
q[rear++]=N;
while(front<rear)
{
n=q[front++];
if(n==K)
break;
if(n<K)
{
newn=n-1;
if(dis[newn]<0)
{
dis[newn]=dis[n]+1;
q[rear++]=newn;
}
newn=n+1;
if(dis[newn]<0)
{
dis[newn]=dis[n]+1;
q[rear++]=newn;
}
newn=2*n;
if(dis[newn]<0)
{
dis[newn]=dis[n]+1;
q[rear++]=newn;
}
}
else
{
newn=n-1;
if(dis[newn]<0)
{
dis[newn]=dis[n]+1;
q[rear++]=newn;
}
}
}
printf("%d\n",dis[K]);
}
return 0;
}