Problem-Solving ideas: Simple wide search, the key is pruning.
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <queue>5 using namespacestd;6 Const intMAXN =100005;7 intVIS[MAXN];//Mark whether this point has passed8 intN, K;9 Ten One structnode{ A intx, CNT; - }p, now; - theQueue<node>Q; - - intBFsintXintCNT) - { +memset (Vis,0,sizeof(VIS));//initialized to never go through - while(!q.empty ()) Q.pop (); +p.x = x, p.cnt =CNT; A Q.push (p); at - while(!q.empty ()) - { -p =Q.front (); - Q.pop (); - in if(p.x = = k)returnp.cnt;//go to that point and return directly to the number of steps - tonow.x = p.x +1, now.cnt = p.cnt +1; + if(Now.x <=100000&&!vis[now.x])//critical pruning, not traversed and within the data range that meets the topic - { theVis[now.x] =1;//marked as having traversed * Q.push (now); $ }Panax Notoginseng -now.x = p.x-1, now.cnt = p.cnt +1; the if(Now.x >=0&&!vis[now.x]) + { AVis[now.x] =1; the Q.push (now); + } - $now.x =2* p.x, now.cnt = p.cnt +1; $ if(Now.x <=100000&&!vis[now.x]) - { -Vis[now.x] =1; the Q.push (now); - }Wuyi } the return-1;//won't come to this step - } Wu - intMain () About { $ while(~SCANF ("%d%d", &n, &k)) - { - intAns = BFS (n,0); -printf"%d\n", ans); A } + return 0; the}
View Code
POJ 3278 Catch that Cow