http://tjuacm.chaosheng.top/problem.php?id=1287
https://www.luogu.com.cn/problem/P3395
思路参考:https://blog.csdn.net/qq_44793348/article/details/109682032

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

const int INF = 5000;
const int N = 1010;

int n;
int matrix[N][N];
bool vis[N][N];
int dx[4] = {0,0,-1,1};
int dy[4] = {1,-1,0,0}; 

struct pos{
    int x, y;
    int time;
};

int bfs(){
    memset(vis, false, sizeof(vis));
    queue<pos> q;
    pos f;
    f.x = 1;
    f.y = 1;
    f.time = 0;
    q.push(f);
    vis[f.x][f.y] = true;

    while(!q.empty()){
        f = q.front();
        q.pop();

        //满足条件
        if(f.x == n && f.y == n){
            return true;
        }

        for(int i = 0; i < 4; i++){
            pos v = f;
            v.x = f.x + dx[i];
            v.y = f.y + dy[i];
            //printf("v  x = %d vy = %d\n", v.x, v.y);
            if(v.x > 0 && v.x <= n && v.y > 0 && v.y <= n && !vis[v.x][v.y] && matrix[v.x][v.y] >= f.time + 1){
                vis[v.x][v.y] = true;
                v.time += 1;
                //printf("v.time = %d\n");
                q.push(v);
            }
        }        
    }
    return false;
}

int main(){
    int t;
    cin >> t;
    while(t--){
        cin >> n;
        int x, y;
        memset(matrix, 0x7f, sizeof(matrix));
        for(int i = 1; i < 2 * n - 1; i++){
            cin >> x >> y;
            matrix[x][y] = i;    
        }
        if(bfs()) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}