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;
}