CF1494B Berland Crossword 题解
CF1494B Berland Crossword 题解
题目描述
题目解法
思路
不难发现,会导致行和列互相干扰的格子只有矩阵角上的四个格子。
共有 \(4\) 个格子,有 \(2^4=16\) 种情况。
可以暴力枚举所有情况。
每次只需要判断该状态下是否符合题意即可。
在这里使用状态压缩实现。
Code
#include<bits/stdc++.h>
using namespace std;
struct st{int n, lx[4];};
bool chk(st a, int k)
{
auto [n, l]=a;
for(int i=0;i<4;i++, k>>=1)
if(k&1) l[i]--, l[(i+3)%4]--;
for(int i=0;i<4;i++)
if(l[i]<0||l[i]>n-2) return 0;
return 1;
}
void solve()
{
int n, u, r, d, l;
cin>>n>>u>>r>>d>>l;
st m={n, {u, r, d, l}};
for(int i=0;i<16;i++)
if(chk(m, i))
return cout<<"YES\n", void();
cout<<"NO\n";
}
int main()
{
int t;
cin>>t;
while (t--) solve();
}

浙公网安备 33010602011771号