Because there are only 1e5 points, so the direct discretization of BFS is good
#include <cstdio>#include<cstring>#include<queue>#include<Set>#include<map>#include<stack>#include<cstdlib>#include<algorithm>#include<vector>#include<cmath>using namespaceStd;typedefLong LongLl;typedef pair<int,int>PII;Const intn=1e5+5;Const intinf=0x3f3f3f3f;Const intMod=1000000007;p II p[n],tmp;intdx[8]={-1,-1,-1,0,0,1,1,1};intdy[8]={-1,0,1,-1,1,-1,0,1};inthead[n],tot,d[n],cnt;structedge{intV,next;} edge[8*N];voidAddintUintv) {EDGE[TOT].V=v; Edge[tot].next=Head[u]; Head[u]=tot++;} Queue<int>Q;int Get(intSintt) { while(!q.empty ()) Q.pop (); memset (d,-1,sizeof(d)); D[s]=0; Q.push (s); while(!Q.empty ()) { intu=Q.front (); Q.pop (); if(u==t)returnD[t]; for(intI=head[u];~i;i=Edge[i].next) { intv=edge[i].v; if(d[v]==-1) d[v]=d[u]+1, Q.push (v); } } return-1;}intMain () {intx1,x2,y1,y2,n,s,t; while(~SCANF ("%d%d%d%d",&x1,&y1,&x2,&y2)) {scanf ("%d",&N); memset (Head,-1,sizeof(head)), cnt=tot=0; for(intI=1; i<=n;++i) { intX,l,r; scanf ("%d%d%d",&x,&l,&R); for(intj=l;j<=r;++j)++cnt,p[cnt].first=x,p[cnt].second=J; } sort (P+1, p+1+CNT); CNT=unique (p+1, p+1+CNT)-p-1; for(intI=1; i<=cnt;++i) { for(intj=0;j<8;++j) {Tmp.first=p[i].first+Dx[j]; Tmp.second=p[i].second+Dy[j]; intPos=lower_bound (p+1, p+1+CNT,TMP)-p; if(pos==cnt+1|| p[pos].first!=tmp.first| | p[pos].second!=Tmp.second)Continue; Add (I,pos); }} Tmp.first=x1,tmp.second=Y1; S=lower_bound (p+1, p+1+CNT,TMP)-p; Tmp.first=x2,tmp.second=Y2; T=lower_bound (p+1, p+1+CNT,TMP)-p; printf ("%d\n",Get(s,t)); } return 0;}
View Code
FZU2235 King's Travel water problem