提交时间:2023-08-14 12:29:21

运行 ID: 98223

#include<bits/stdc++.h> using namespace std; #define ll long long #define N 2005 int cx[N],cy[N]; bool vis[N][N]; int x[N],y[N]; int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1}; struct node{ int x,y,d; }; queue<node>q; int main(){ int n,m; scanf("%d%d",&n,&m); for(int i=1;i<n;i++) scanf("%d%d",&x[i],&y[i]),cx[x[i]]=i,cy[y[i]]=i; while(m--){ int op,a,b,c,d; scanf("%d",&op); if(op==1){ scanf("%d%d%d",&a,&b,&c); cx[x[a]]=0,cy[y[a]]=0; x[a]=b,y[a]=c; cx[x[a]]=a,cy[y[a]]=a; }else{ memset(vis,0,sizeof(vis)); while(!q.empty())q.pop(); scanf("%d%d%d%d",&a,&b,&c,&d); q.push({a,b,0}); vis[a][b]=1; while(!q.empty()){ int x=q.front().x,y=q.front().y,p=q.front().d; q.pop(); if(x==c&&y==d){ printf("%d\n",p); break; } for(int i=0;i<4;i++){ int xx=dx[i]+x,yy=dy[i]+y; bool lf(0); if(xx<1||yy<1||xx>n||yy>n)continue; if(cx[y]==x&&dx[i]==1)lf=1; if(cy[x]==y&&dy[i]==-1)lf=1; if(cx[yy]==xx&&dx[i]==-1)lf=1; if(cy[xx]==yy&&dy[i]==-1)lf=1; if(max(xx,yy)==max(x,y))lf=1; if(lf) if(!vis[xx][yy]){ vis[xx][yy]=1; q.push({xx,yy,p+1}); } } } } } return 0; }