poj3278
#include<iostream> #define MAX 100001 int john,cow; int queue[MAX]; int vis[MAX]; int ans; void bfs() { int tail,head; tail=head=0; int start=john; queue[tail++]=start; vis[start]=1; while(head!=tail) { int cur,next; cur=queue[head++]; if(cur==cow) { ans=vis[cur]-1; return; } next=cur; next=cur+1; if(next>=0&&next<MAX&&vis[next]==0) { vis[next]=vis[cur]+1; queue[tail++]=next; } next=cur-1; if(next>=0&&next<MAX&&vis[next]==0) { vis[next]=vis[cur]+1; queue[tail++]=next; } next=2*cur; if(next>=0&&next<MAX&&vis[next]==0) { vis[next]=vis[cur]+1; queue[tail++]=next; } } } int main() { //freopen("input.txt","r",stdin); std::cin>>john>>cow; for(int i=0;i<MAX;i++) vis[i]=0; bfs(); std::cout<<ans; return 0; }