bzoj1646[usaco2007 open]catch that Cow catch the cow *

Source: Internet
Author: User

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 *

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.