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