bzoj3383[usaco2004 Open]cave Cows 4 The four cows in the cave

Source: Internet
Author: User

bzoj3383[usaco2004 Open]cave Cows 4 The four cows in the cave

Test instructions

The plane Cartesian coordinate system has n points, starting from (0,0), from one point can jump to all the points with its horizontal ordinate distance ≤2, the minimum number of steps so that the ordinate is t.

Exercises

Save all the points with set first. When doing DP, all the horizontal ordinate and the current node distance ≤2 node is found in set, if you can find it can be transferred to that node.

Code:

1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <Set>5#include <queue>6 #defineMAXN 500107 #defineInc (I,J,K) for (int i=j;i<=k;i++)8 using namespacestd;9 TenInlineintRead () { One     CharCh=getchar ();intf=1, x=0; A      while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; Ch=GetChar ();} -      while(ch>='0'&&ch<='9') x=x*Ten+ch-'0', ch=GetChar (); -     returnf*x; the } - intX[MAXN],Y[MAXN],N,T,DIS[MAXN]; queue<int>Q; - structnd{ -     intX,y,id; +     BOOL operator< (Constnd &a)Const{returnx!=a.x?x<a.x:y<a.y;} - }; + Set<nd>St; A intMain () { atN=read (); T=read (); Inc (I,1, n) x[i]=read (), y[i]=read (), St.insert (nd) {x[i],y[i],i}); -Q.push (0); dis[0]=0; -      while(!Q.empty ()) { -         intz=Q.front (); Q.pop (); -Inc (i,-2,2) Inc (j,-2,2){ -             Set<nd>::iterator a=st.find (nd) {x[z]+i,y[z]+j,0}); in             if(a!=St.end ()) { -dis[a->id]=dis[z]+1; Q.push (a->ID); to                 if(a->y==t) {printf ("%d", Dis[a->id]);return 0;} St.erase (a); +             } -         } the     } *printf"-1");return 0; $}

20160912

bzoj3383[usaco2004 Open]cave Cows 4 The four cows in the cave

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.