P3395
Gua20131127 · · 休闲·娱乐
#include<bits/stdc++.h>
using namespace std;
struct node{
int x , y , step;
};
int n;
int s[1010][1010];
int vis[1010][1010];
int d[4][2] = {{-1 , 0} , {0 , -1} , {0 , 1} , {1 , 0}};
int x , y;
bool f = false;
void bfs(int x , int y){
queue<node>q;
vis[1][1] = 0;
q.push({1 , 1 , 0});
while(q.size()){
node t = q.front();
q.pop();
if(t.x == n && t.y == n){
f = true;
return ;
}
for(int i = 0;i < 4;i ++){
int px = t.x + d[i][0];
int py = t.y + d[i][1];
if(px < 1 || px > n || py < 1 || py > n){
continue;
}
if(vis[px][py] != -1){
continue;
}
if(s[px][py] <= t.step){
continue;
}
vis[px][py] = vis[t.x][t.y] + 1;
q.push({px , py , t.step + 1});
}
}
}
int main(){
int T;
cin >> T;
while(T --){
cin >> n;
for(int i = 1;i <= n;i ++){
for(int j = 1;j <= n;j ++){
s[i][j] = INT_MAX;
vis[i][j] = -1;
}
}
for(int i = 1;i <= 2 * n - 2;i ++){
cin >> x >> y;
s[x][y] = i;
f = 0;
}
bfs(1 , 1);
if(f){
cout << "Yes" << endl;
}else{
cout << "No" << endl;
}
}
return 0;
}