EDU105|codeforces1494B. Berland Crossword

B. Berland Crossword time limit per test2 seconds memory limit per
test256 megabytes inputstandard input outputstandard output Berland
crossword is a puzzle that is solved on a square grid with n rows and
n columns. Initially all the cells are white.

To solve the puzzle one has to color some cells on the border of the
grid black in such a way that:

exactly U cells in the top row are black; exactly R cells in the
rightmost column are black; exactly D cells in the bottom row are
black; exactly L cells in the leftmost column are black. Note that you
can color zero cells black and leave every cell white.

Your task is to check if there exists a solution to the given puzzle.

Input The first line contains a single integer t (1≤t≤1000) — the
number of testcases.

Then the descriptions of t testcases follow.

The only line of each testcase contains 5 integers n,U,R,D,L (2≤n≤100;
0≤U,R,D,L≤n).

Output For each testcase print “YES” if the solution exists and “NO”
otherwise.

You may print every letter in any case you want (so, for example, the
strings yEs, yes, Yes and YES are all recognized as positive answer).

Example inputCopy 4 5 2 5 3 1 3 0 0 0 0 4 4 1 4 0 2 1 1 1 1 outputCopy
YES YES NO YES Note Here are possible solutions to testcases 1, 2 and
4

这里是引用
我们可以观察发现 问题的关键在于上下左右四角的状态,要关注n-2个以外行的情况
然而强行模拟的话边界状态很棘手。(我太菜了)
因为只要考虑4个点的状态,枚举一下就可以了,每次减去对应的两个值
四位数字分别对应 UR DR DL UL

//cyc
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define mst(a) memset(a,0,sizeof a)

using namespace std;
typedef pair<int, int> pii;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int _;
    cin >> _;
    while(_--)
    {
        int n;
        cin>>n;
        vector<int> a(4), b(4);
        for(int i = 0; i < 4; i++)
        {
            cin >> a[i];
        }
        int flag=1;
        for(int i = 0; i < (1 << 4); i++) //10000
        {
            b=a;
            int cur=i;
            for(int i=0;i<4;i++){
                if((cur>>i)&1){
                    b[i]--;
                    b[(i+1)%4]--;
                }
            }
            flag=1;
            for(int i=0;i<4;i++){
                if(b[i]>n-2)flag=0;
                if(b[i]<0)flag=0;
            }
            if(flag){
                break;
            }
        }
        if(flag)cout<<"YES"<<endl;
        else cout<<"NO"<<endl;

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值