FZU2235 国王的出游 水题

因为只有1e5个点,所以直接离散化bfs就好

#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
typedef long long LL;
typedef pair<int,int>pii;
const int N=1e5+;
const int INF=0x3f3f3f3f;
const int mod=;
pii p[N],tmp;
int dx[]={-,-,-,,,,,};
int dy[]={-,,,-,,-,,};
int head[N],tot,d[N],cnt;
struct Edge{
int v,next;
}edge[*N];
void add(int u,int v){
edge[tot].v=v;
edge[tot].next=head[u];
head[u]=tot++;
}
queue<int>q;
int get(int s,int t){
while(!q.empty())q.pop();
memset(d,-,sizeof(d));
d[s]=;q.push(s);
while(!q.empty()){
int u=q.front();
q.pop();
if(u==t)return d[t];
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].v;
if(d[v]==-)d[v]=d[u]+,q.push(v);
}
}
return -;
}
int main(){
int x1,x2,y1,y2,n,s,t;
while(~scanf("%d%d%d%d",&x1,&y1,&x2,&y2)){
scanf("%d",&n);
memset(head,-,sizeof(head)),cnt=tot=;
for(int i=;i<=n;++i){
int x,l,r;
scanf("%d%d%d",&x,&l,&r);
for(int j=l;j<=r;++j)
++cnt,p[cnt].first=x,p[cnt].second=j;
}
sort(p+,p++cnt);
cnt=unique(p+,p++cnt)-p-;
for(int i=;i<=cnt;++i){
for(int j=;j<;++j){
tmp.first=p[i].first+dx[j];
tmp.second=p[i].second+dy[j];
int pos=lower_bound(p+,p++cnt,tmp)-p;
if(pos==cnt+||p[pos].first!=tmp.first||p[pos].second!=tmp.second)
continue;
add(i,pos);
}
}
tmp.first=x1,tmp.second=y1;
s=lower_bound(p+,p++cnt,tmp)-p;
tmp.first=x2,tmp.second=y2;
t=lower_bound(p+,p++cnt,tmp)-p;
printf("%d\n",get(s,t));
}
return ;
}
上一篇:java基础-jdk工具包


下一篇:solr的增删改查