Note: My English is very slag, the main idea is mostly from Baidu ~=0= the topic of farmers John needs to seize his cattle, he and his cattle in a straight line (estimated to be one-dimensional creatures), John in N (0≤n≤100,000), his cattle in K (0≤k≤100,000), John Next can move from X to X +1 or x-1 or 2*x, ask John how many steps he needs at least to find his cow. With BFS can be solved ~ but need pruning or will be the MLE is worth mentioning is the number of steps do not use the structure of the preservation maybe I am not good enough to prune the structure to save the MLE
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <queue>5#include <cstdlib>6#include <cmath>7#include <cctype>8 #defineN 2000109 using namespacestd;Ten One BOOLMaps[n];//used to judge this point John has not walked A intA, B, step[n];//A, B is N, K, respectively ,//Step[i] Used to save John's walk to point I took a few steps (note: Do not use the structure to save the number of steps, otherwise it may be MLE) - - voidBFS () the { - intQ1 =A; -Maps[a] =true; -queue<int>Q; + Q.push (Q1); - + while(!Q.empty ()) { A intQ2 =Q.front (); at Q.pop (); - if(q2 = = b) {//the output went a few steps after I found it. -printf"%d\n", STEP[Q2]); - return ; - } - //John, go--1 only if he has a positive coordinate at this point . in intx = Q2-1; - if(!maps[x] && Q2 >0) { toMAPS[X] =true; +Q1 =x; -STEP[Q1] = step[q2] +1; the Q.push (Q1); * } $ //as John walked backwards only-1 so only Q2 < B was moving forward.Panax Notoginseng if(Q2 <b) { -x = q2 +1; the if(!Maps[x]) { +MAPS[X] =true; AQ1 =x; theSTEP[Q1] = step[q2] +1; + Q.push (Q1); - } $x = q2 *2; $ if(!Maps[x]) { -MAPS[X] =true; -Q1 =x; theSTEP[Q1] = step[q2] +1; - Q.push (Q1);Wuyi } the } - Wu } - } About $ intMain () - { - while(~SCANF ("%d%d", &a, &b)) { -memset (Maps,0,sizeof(maps)); A BFS (); + } the return 0; -}
POJ 3278 Catch that Cow