CF1494B Berland Crossword 题解

CF1494B Berland Crossword 题解

题目描述

CF1494B Berland Crossword

题目解法

思路

不难发现,会导致行和列互相干扰的格子只有矩阵角上的四个格子。

共有 4 个格子,有 24=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();
}

本文作者:Jimmy-LEEE

本文链接:https://www.cnblogs.com/redacted-area/p/18379524

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Jimmy-LEEE  阅读(5)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起