bzoj1646[usaco2007 open]catch that Cow catch the cow
Test instructions
On the axis, the starting point in N, the end point in K, each walk can go to the left one step or to the right step or teleport to the current coordinates of twice times, ask at least a few times. 0≤n,k≤100000.
Exercises
BFS, the permissible walking position boundary is [0,max (n,k) +1]. The Nether is 0 because if you go to a position less than 0, k≥0, then teleport and go left are the opposite, only to the right, then the first should not go to less than 0 of the position resulting in a waste of time. The upper bound for Max (n,k) +1 is because if you go to a position greater than this number, K must be smaller than the current position, then you have to step back, and it is obviously more time than before to go to this position ... (I admit I'm a mess) so the complexity is 100000.
Code:
1#include <cstdio>2#include <algorithm>3#include <cstring>4#include <queue>5 #defineMAXN 1001006 #defineInc (I,J,K) for (int i=j;i<=k;i++)7 using namespacestd;8 9Queue <int> q;inttim[maxn],n,k,mx;Ten intMain () { Onescanf"%d%d", &n,&k); memset (tim,-1,sizeof(Tim)); Q.push (n); tim[n]=0; Mx=max (n,k) +1; A while(!Q.empty ()) { - intx=Q.front (); Q.pop (); - if(x+1<=mx&&tim[x+1]==-1) {Q.push (x+1); tim[x+1]=tim[x]+1;if(x+1==K) Break;} the if(X-1>=0&&tim[x-1]==-1) {Q.push (X-1); tim[x-1]=tim[x]+1;if(X-1==K) Break;} - if(x<<1<=mx&&tim[x<<1]==-1) {Q.push (x<<1),tim[x<<1]=tim[x]+1;if(x<<1==K) Break;} - } -printf"%d", Tim[k]);return 0; +}
20160802
bzoj1646[usaco2007 open]catch that Cow catch the cow *