Berland Crossword

Berland crossword is a puzzle that is solved on a square grid with nn rows and nn 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 UU cells in the top row are black;
  • exactly RR cells in the rightmost column are black;
  • exactly DD cells in the bottom row are black;
  • exactly LL 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 tt (1≤t≤10001≤t≤1000) — the number of testcases.

Then the descriptions of tt testcases follow.

The only line of each testcase contains 55 integers n,U,R,D,Ln,U,R,D,L (2≤n≤1002≤n≤100; 0≤U,R,D,L≤n0≤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).

Input:

4
5 2 5 3 1
3 0 0 0 0
4 4 1 4 0
2 1 1 1 1

Out:

YES
YES
NO
YES

思路:枚举四个角有没有被占用的情况,只要有一种符合条件即可

如何才叫符合呢?即除了四个角之外,四个边是否出界,是否不足

完整代码:

#include<bits/stdc++.h>
#define int long long 
#define rep(i, a, b) for(int i = a; i <= b; ++ i)
#define per(i, a, b) for(int i = a; i >= b; -- i)
using namespace std;
const int N = 1e5 + 10,mod = 1e9 + 7;
int n,u,r,d,l;

bool work(int i,int j,int k,int z)
{
     if(u > i+j+n-2 || u<i+j) return false;
     if(l > i+k+n-2 || l<i+k) return false;
     if(d > k+z+n-2 || d<k+z) return false;
     if(r > j+z+n-2 || r<j+z) return false;
     return true;
}

void solve()
{
        cin>>n>>u>>r>>d>>l;
        for(int i=0 ; i<=1 ; i++)
            for(int j=0 ; j<=1 ; j++)
                for(int k=0 ; k<=1 ; k++)
                    for(int z=0 ; z<=1 ; z++)
                    {
                        if(work(i,j,k,z)){
                            cout<<"YES"<<endl;
                            return ;
                        }
                    }
        cout<<"NO"<<endl;
}
signed main()
{
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值