1#include <cstdio>2#include <queue>3#include <cstring>4 using namespacestd;5 Const intmaxn=200010;6 intn,k;7 BOOLV[MAXN];8 struct Point9 {Ten intx; One intStep; A }; - intBFS () - { thememset (V,0,sizeof(v)); -Queue<point>que; - Point now,next; -now.x=N; +now.step=0; -v[n]=1; + Que.push (now); A at while(!que.empty ()) - { -now=Que.front (); - Que.pop (); - if(now.x==k) Break; - innext.x=now.x+1; - if(next.x>=0&& NEXT.X<=MAXN &&!V[next.x]) to { +v[next.x]=1; -next.step=now.step+1; the Que.push (next); * } $ Panax Notoginsengnext.x=now.x-1; - if(next.x>=0&& NEXT.X<=MAXN &&!V[next.x]) the { +v[next.x]=1; Anext.step=now.step+1; the Que.push (next); + } - $next.x=now.x*2; $ if(next.x>=0&& NEXT.X<=MAXN &&!V[next.x]) - { -v[next.x]=1; thenext.step=now.step+1; - Que.push (next);Wuyi } the - } Wu returnNow.step; - } About $ intMain () - { - while(SCANF ("%d%d", &n,&k)! =EOF) - { Aprintf"%d\n", BFS ()); + } the return 0; -}
Bfs/poj 3278 Catch that Cow