官方题解
https://codeforces.com/blog/entry/88344
对矩形四个角进行状压即可,然后对每条边剩余的n - 2个位置枚举即可
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
#define IO std::ios::sync_with_stdio(false); cin.tie(0)
#define ll long long
#define ull unsigned long long
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define per(i, l, r) for (int i = l; i >= r; --i)
#define mset(s, _) memset(s, _, sizeof(s))
#define mcpy(s, _) memcpy(s, _, sizeof(s))
#define pb push_back
#define pii pair <int, int>
#define vi vector<int>
#define vpii vector<pii>
#define mp(a, b) make_pair(a, b)
#define debug system("pause")
#define pll pair <ll, ll>
#define fir first
#define sec second
#define inf 0x3f3f3f3f
#define pline cout << endl << endl
inline int lowbit(int x) {return x & -x;}
inline bool cmp(int a, int b) {return a > b;}
template< typename T > inline void get_min(T &x, T y) {if(y < x) x = y;}
template< typename T > inline void get_max(T &x, T y) {if(x < y) x = y;}
inline int read() {
int x = 0, f = 0; char ch = getchar();
while (!isdigit(ch)) f |= ch == '-', ch = getchar();
while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar();
return f ? -x : x;
}
template<typename T> inline void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) print(x / 10);
putchar(x % 10 + '0');
}
template<typename T> inline void print(T x, char let) {
print(x), putchar(let);
}
const int N = 110;
bool st[N][N];
int t, n, u, r, d, l;
/*
对矩形四个角进行状压即可,然后对每条边剩余的n - 2个位置枚举即可
*/
int main() {
cin >> t;
while(t -- ) {
mset(st, 0);
cin >> n >> u >> r >> d >> l;
bool flag = 0;
for(int i = 0; i <= 15; i ++ ) {
int uu = u, rr = r, dd = d, ii = l;
mset(st, 0);
for(int j = 0; j <= 3; j ++ ) {
if(j == 0) {
st[0][0] = i >> j & 1;
if(st[0][0]) {
uu -- , ii -- ;
}
}
if(j == 1) {
st[0][n - 1] = i >> j & 1;
if(st[0][n - 1]) {
uu -- , rr -- ;
}
}
if(j == 2) {
st[n - 1][0] = i >> j & 1;
if(st[n - 1][0]) {
dd -- , ii -- ;
}
}
if(j == 3) {
st[n - 1][n - 1] = i >> j & 1;
if(st[n - 1][n - 1]) {
dd -- , rr -- ;
}
}
}
if(uu < 0 || rr < 0 || dd < 0 || ii < 0) continue;
for(int j = 1; j <= n - 2; j ++ ) {
if(!st[0][j] && uu) {
st[0][j] = 1; uu -- ;
}
if(!st[j][n - 1] && rr) {
st[j][n - 1] = 1; rr -- ;
}
if(!st[j][0] && ii) {
st[j][0] = 1; ii -- ;
}
if(!st[n - 1][j] && dd) {
st[n - 1][j] = 1; dd -- ;
}
}
int ansu = 0, ansr = 0, ansd = 0, ansl = 0;
for(int j = 0; j < n; j ++ ) {
if(st[0][j]) ansu ++ ;
if(st[j][n - 1]) ansr ++ ;
if(st[j][0]) ansl ++ ;
if(st[n - 1][j]) ansd ++ ;
}
if(ansu == u && ansr == r && ansd == d && ansl == l) {
flag = 1;
break;
}
}
if(flag) puts("YES");
else puts("NO");
}
return 0;
}