题面
https://www.luogu.org/problem/P2537
题解
#include<cstdio> #include<iostream> #include<queue> #include<algorithm> #include<cstring> #define ri register int #define N 105 using namespace std; const int dx[]={0,1,0,-1},dy[]={1,0,-1,0}; int n,c; int sx,sy,tx,ty,cx,cy; int x1[N],y1[N],x2[N],y2[N]; int nx1[N],ny1[N],nx2[N],ny2[N]; int tr1[N<<1],tr2[N<<1]; struct node { int x; bool s; int id; bool operator < (const node &rhs) const { return x<rhs.x; } } a[N<<1]; bool in[N][2*N][2*N]; bool vis[2*N][2*N]; struct nod { int x,y,s; }; deque<nod> q; void get(int &a,int &b,int x,int y){ for (ri i=1;i<cx;i++) for (ri j=1;j<cy;j++) if (tr1[i]<=x && x<=tr1[i+1] && tr2[j]<=y && y<=tr2[j+1]) { a=i; b=j; return; } } int daijia(int x1,int y1,int x2,int y2){ for (ri i=1;i<=n;i++) { if ((in[i][x1][y1] && !in[i][x2][y2])||(!in[i][x1][y1] && in[i][x2][y2])) return 1; } return 0; } int main(){ scanf("%d",&n); for (ri i=1;i<=n;i++) { scanf("%d %d %d",&x1[i],&y1[i],&c); x2[i]=x1[i]+c; y2[i]=y1[i]+c; } scanf("%d %d %d %d",&sx,&sy,&tx,&ty); for (ri i=1;i<=n;i++) a[2*i-1]=(node){x1[i],0,i},a[2*i]=(node){x2[i],1,i}; n++; a[2*n-1]=(node){-1,0,n}; a[2*n]=(node){100000,0,n}; sort(a+1,a+2*n+1); cx=0; for (ri i=1;i<=2*n;i++) { if (a[i].x!=a[i-1].x) { cx++; tr1[cx]=a[i].x; } if (!a[i].s) nx1[a[i].id]=cx; else nx2[a[i].id]=cx; } //for (ri i=1;i<=cx;i++) cout<<tr1[i]<<" "; cout<<endl; n--; for (ri i=1;i<=n;i++) a[2*i-1]=(node){y1[i],0,i},a[2*i]=(node){y2[i],1,i}; n++; a[2*n-1]=(node){-1,0,n}; a[2*n]=(node){100000,0,n}; sort(a+1,a+2*n+1); cy=0; for (ri i=1;i<=2*n;i++) { if (a[i].x!=a[i-1].x) { cy++; tr2[cy]=a[i].x; } if (!a[i].s) ny1[a[i].id]=cy; else ny2[a[i].id]=cy; } n--; //for (ri i=1;i<=cy;i++) cout<<tr2[i]<<" "; cout<<endl; //puts("83"); //for (ri i=1;i<=n;i++) printf("%d %d %d %d ",nx1[i],nx2[i],ny1[i],ny2[i]); for (ri i=1;i<=n;i++) for (ri x=1;x<cx;x++) for (ri y=1;y<cy;y++) if (nx1[i]<=x && x+1<=nx2[i] && ny1[i]<=y && y+1<=ny2[i]) { in[i][x][y]++; //cout<<"87 "<<i<<" "<<x<<" "<<y<<endl; } //puts("80"); int fx,fy,ex,ey; get(fx,fy,sx,sy); get(ex,ey,tx,ty); //cout<<fx<<" "<<fy<<endl; //cout<<ex<<" "<<ey<<endl; q.push_back((nod){fx,fy,0}); vis[fx][fy]=1; while (!q.empty()) { nod cur=q.front(); q.pop_front(); for (ri i=0;i<4;i++) { int nx=cur.x+dx[i],ny=cur.y+dy[i]; if (nx<cx && ny<cy && nx>=1 && ny>=1 && !vis[nx][ny]) { vis[nx][ny]=1; int t=daijia(cur.x,cur.y,nx,ny); if (!t) q.push_front((nod){nx,ny,cur.s+daijia(cur.x,cur.y,nx,ny)}); else q.push_back((nod){nx,ny,cur.s+daijia(cur.x,cur.y,nx,ny)}); if (nx==ex && ny==ey) { printf("%d ",cur.s+daijia(cur.x,cur.y,nx,ny)); return 0; } } } } puts("-1"); return 0; }