RE4个 WA1个
#include<bits/stdc++.h>
using namespace std;
const int N=1005;
queue<int>q1;
queue<int>q2;
int g[N][N];
int lx[N],ly[N];
bool st[N][N],f;
int dx[5]={-1,0,1,0};
int dy[5]={0,1,0,-1};
int n,j;
int tx,ty;
void bfs(int x,int y){
q1.push(x);
q2.push(y);
for(int z=1;q1.size();z++){
for(int i=0;i<4;i++){
tx=q1.front()+dx[i];
ty=q2.front()+dy[i];
if(tx<1 || tx>n || ty<1 || ty>n)continue;
if(!st[tx][ty] && !g[tx][ty]){
q1.push(tx);
q2.push(ty);
st[tx][ty]=1;
}
g[lx[++j]][ly[j]]=1;
// for(int i=1;i<=n;i++){
// for(int j=1;j<=n;j++){
// cout<<g[i][j];
// }
// cout<<endl;
// }
// cout<<endl;
if(tx==n && ty==n){
f=1;
return;
}
}
if(f)break;
q1.pop();
q2.pop();
}
return;
}
int main(){
int t;
cin>>t;
while(t--){
f=0;
while(!q1.empty())q1.pop(),q2.pop();
memset(g,0,sizeof(g));
memset(st,0,sizeof(st));
memset(lx,0,sizeof(lx));
memset(ly,0,sizeof(ly));
cin>>n;
for(int i=1;i<=n;i++){
cin>>lx[i]>>ly[i];
}
st[1][1]=1;
bfs(1,1);
if(f)cout<<"YES";
else cout<<"NO";
}
}