提交时间:2023-08-14 13:19:14
运行 ID: 98254
#include<bits/stdc++.h> #define int long long using namespace std; inline int read() { int f=1,x=0; char c=getchar(); while(c>'9'||c<'0') { if(c=='-') f*=-1; c=getchar(); } while(c>='0'&&c<='9') { x=x*10+c-'0'; c=getchar(); } return x*f; } const int N=109; int a[N][N][4],v[N][N]; int dx[4]={0,0,1,-1}; int dy[4]={1,-1,0,0}; int edx,edy; int q1[N*N],q2[N*N],q3[N*N],head,tail; int bfs(int stx,int sty,int edx,int edy) { head=1; tail=0; memset(v,0,sizeof(v)); q1[++tail]=stx; q2[tail]=sty; q3[tail]=0; v[stx][sty]=1; while(head<=tail) { if(q1[head]==edx&&q2[head]==edy) { return q3[head]; } for(int i=0;i<4;i++) { if(a[q1[head]][q2[head]][i]==0) continue; if(v[q1[head]+dx[i]][q2[head]+dy[i]]) continue; q1[++tail]=q1[head]+dx[i]; q2[tail]=q2[head]+dy[i]; q3[tail]=q3[head]+1; } head++; } return 114514; } int mx[N],my[N],n,m; signed main() { n=read(); m=read(); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { if(i==j&&i!=1) { a[i][j][1]=a[i][j][3]=1; } if(i>j) { if(j!=1) a[i][j][1]=1; a[i][j][0]=1; } if(i<j) { if(i!=1) a[i][j][3]=1; a[i][j][2]=1; } } for(int i=1;i<n;i++) { mx[i]=read(); my[i]=read(); a[i][mx[i]][2]=1; a[i+1][mx[i]][3]=1; a[my[i]][i][0]=1; a[my[i]][i+1][1]=1; } while(m--) { int opt=read(); if(opt==1) { int i=read(),x=read(),y=read(); a[i][mx[i]][2]=0; a[i+1][mx[i]][3]=0; a[my[i]][i][0]=1; a[my[i]][i+1][1]=1; mx[i]=x; my[i]=y; a[i][mx[i]][2]=1; a[i+1][mx[i]][3]=1; a[my[i]][i][0]=1; a[my[i]][i+1][1]=1; } else { printf("%lld\n",bfs(read(),read(),read(),read())); } } return 0; }