题目
![](https://i-blog.csdnimg.cn/direct/66938fe616234625bf6631eb8530ca58.png)
代码
#include <bits/stdc++.h>
using namespace std;
using pll = pair<int, int>;
#define x first
#define y second
const int N = 51;
pll d[4][4][4] = {{{{0, 0}, {1, 0}, {2, 0}, {2, 1}}, {{0, 0}, {1, 0}, {1, -1}, {1, -2}}, {{0, 0}, {0, 1}, {1, 1}, {2, 1}}, {{0, 0}, {0, 1}, {0, 2}, {1, 0}}},{{{0, 0}, {1, 0}, {2, 0}, {3, 0}}, {{0, 0}, {0, 1}, {0, 2}, {0, 3}}, {{0, 0}, {1, 0}, {2, 0}, {3, 0}}, {{0, 0}, {0, 1}, {0, 2}, {0, 3}}},{{{0, 0}, {0, 1}, {0, 2}, {1, 1}}, {{0, 0}, {1, 0}, {2, 0}, {1, 1}}, {{0, 0}, {1, 0}, {1, -1}, {1, 1}}, {{0, 0}, {1, 0}, {1, -1}, {2, 0}}},{{{0, 0}, {0, 1}, {1, 0}, {1, -1}}, {{0, 0}, {1, 0}, {1, 1}, {2, 1}}, {{0, 0}, {0, 1}, {1, 0}, {1, -1}}, {{0, 0}, {1, 0}, {1, 1}, {2, 1}}}};
bool g[N][N];
int n, tar;bool dfs(int x, int y, int st)
{if (st == tar)return true;if (y > n)return dfs(x + 1, 1, st);if (x > n)return false;for (int i = 0; i < 4; i++){if ((st >> i) & 1)continue;for (int j = 0; j < 4; j++){if ((st >> i) & 1)break;bool bj = true;for (int k = 0; k < 4; k++){int nx = x + d[i][j][k].x;int ny = y + d[i][j][k].y;if (nx < 1 || ny < 1 || nx > n || ny > n || !g[nx][ny]){bj = false;break;}}if (bj){for (int k = 0; k < 4; k++){int nx = x + d[i][j][k].x;int ny = y + d[i][j][k].y;g[nx][ny] = 0;}if (dfs(x, y + 1, st | (1 << i)))return true;for (int k = 0; k < 4; k++){int nx = x + d[i][j][k].x;int ny = y + d[i][j][k].y;g[nx][ny] = 1;}}}}return dfs(x, y + 1, st);
}int main()
{ios::sync_with_stdio(0);cin.tie(0);int t;cin >> t;tar = (1 << 4) - 1;while (t--){cin >> n;for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++)cin >> g[i][j];if (dfs(1, 1, 0))cout << "Yes\n";elsecout << "No\n";}
}