Berland Crossword cf1494B 康康子yyds
#include<bits/stdc++.h>
using namespace std;
const int N=110;
int f, n;
void dfs(int u,int r,int d,int l, int idx){
if(idx == 4){
if(u >= 0 && u <= n - 2 && l >= 0 && l <= n - 2 && r >= 0 && r <= n - 2 && d >= 0 && d <= n - 2) f = 1;
return ;
}
dfs(u, r, d, l, idx + 1);
if(idx == 0) dfs(u - 1, r - 1, d, l, idx + 1);
else if(idx == 1) dfs(u - 1, r, d, l - 1, idx + 1);
else if(idx == 2) dfs(u, r - 1, d - 1, l, idx + 1);
else if(idx == 3) dfs(u, r, d - 1, l - 1, idx + 1);
}
int main(){
int t;cin>>t;
while(t--){
int u,r,d,l;
f = 0;
cin>>n>>u>>r>>d>>l;
dfs(u, r, d, l, 0);
if(f)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
十五种情况骗我一一列举
#include<bits/stdc++.h>
using namespace std;
const int N=110;
int n;
int a[N][N];
int u[N],d[N],l[N],r[N];
int dfs(int u,int r,int d,int l){
if(l>=0&&l<=n - 2&&r>=0&&r<=n-2&&u>=0&&u<=n-2&&d>=0&&d<=n-2)return true;
if(l>=1&&l<=n-1&&u>=1&&u<=n-1&&r>=0&&r<=n-2&&d>=0&&d<=n-2)return true;
if(r>=1&&r<=n-1&&u>=1&&u<=n-1&&l>=0&&l<=n-2&&d>=0&&d<=n-2)return true;
if(r>=1&&r<=n-1&&d>=1&&d<=n-1&&l>=0&&l<=n-2&&u>=0&&u<=n-2)return true;
if(l>=1&&l<=n-1&&d>=1&&d<=n-1&&r>=0&&r<=n-2&&u>=0&&u<=n-2)return true;
if(l>=1&&l<=n-1&&u>=2&&u<=n&&r>=1&&r<=n-1&&d>=0&&d<=n-2)return true;
if(l>=1&&l<=n-1&&u>=1&&u<=n-1&&r>=1&&r<=n-1&&d>=1&&d<=n-1)return true;//2
if(l>=2&&l<=n&&u>=1&&u<=n-1&&r>=0&&r<=n-2&&d>=1&&d<=n-1)return true;
if(l>=0&&l<=n-2&&u>=1&&u<=n-1&&r>=2&&r<=n&&d>=1&&d<=n-1)return true;
//if(l>=1&&l<=n-1&&u>=1&&u<=n-1&&r>=1&&r<=n-1&&d>=1&&d<=n-1)同2
if(l>=1&&l<=n-1&&u>=0&&u<=n-2&&r>=1&&r<=n-1&&d>=2&&d<=n)return true;
if(l>=2&&l<=n&&u>=2&&u<=n&&r>=2&&r<=n&&d>=2&&d<=n)return true;
if(l>=1&&l<=n-1&&u>=2&&u<=n&&r>=2&&r<=n&&d>=1&&d<=n-1)return true;
if(l>=2&&l<=n&&u>=2&&u<=n&&r>=1&&r<=n-1&&d>=1&&d<=n-1)return true;
if(l>=2&&l<=n&&u>=1&&u<=n-1&&r>=1&&r<=n-1&&d>=2&&d<=n)return true;
if(l>=1&&l<=n-1&&u>=1&&u<=n-1&&r>=2&&r<=n&&d>=2&&d<=n)return true;
return 0;
}
int main(){
int t;cin>>t;
while(t--){
int u,r,d,l;
cin>>n>>u>>r>>d>>l;
if(dfs(u,r,d,l))cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}