Edu Cf Round 105 (Div. 2) B. Berland Crossword 1.读懂题, 2. 思维
一. 原题链接 https://codeforces.com/contest/1494/problem/B
二. 题意 + 题解:
没看懂题目, 懵了好久,
先狡辩一下当时误解的句子, 英语是硬伤了, 呜呜
exactly U cells in the top row are black; //意为 : 最上行恰好有U个黑块
Note that you can color zero cells black and leave every cell white. // 意为 : 你可以一个方格也不涂黑色, 并且可以都涂白色
所以 U,R,D,L分别代表 最上行, 最右列, 最下行, 最左列的要求的黑块数目, 我们要做的就是该咋涂色, 使这2行2列满足.
需要注意的是: 四个角里的颜色连着一行一列, 需分开讨论:
枚举每个角的黑色格子数(0或1),
如果这一行(或列) 两角中黑格数目 <= 目标 && 可以涂黑色的数目 >= 目标,
上下左右四趟都满足则输出YES, 那如何满足呢?
1. 这一行(或列) 两角中黑格数目 <= 目标 : 若为行 则需 最左格+最右格黑格数目 <= 目标;
若为列 则需最上格加最下格黑格数目 <= 目标
2. 可以涂黑色的数目 >= 目标 : 该行(或列)的格子数 - 2 + 两角中黑格数目;
其中该行(或列)的格子数n - 2 意为这一行(或列)不受其它列(或行)的干扰的格子数目
只需将不符合条件的都踢出去就好了, 来个continue
三. AC代码
#include <iostream> using namespace std; int main() { int t, n, u, r, l, d; cin >> t; while(t --) { bool ok = 0; cin >> n >> u >> r >> d >> l;
//核心代码, 看着麻烦, 其实都是类似的代码 for(int ul = 0; ul < 2; ul ++)//ul, ur, dl, dr分别为四个角的黑块数 for(int ur = 0; ur < 2; ur ++) for(int dr = 0; dr < 2;dr ++) for(int dl = 0; dl < 2; dl ++) { if(dr + ur > r || n - 2 + dr + ur < r)//如果这一行两角中黑格数目 <= 目标 && 可以涂黑色的数目 >= 目标 continue; if(ul + dl > l || n - 2 + ul + dl < l) continue; if(ul + ur > u || n - 2 + ul + ur < u) continue; if(dl + dr > d || n - 2 + dl + dr < d) continue; ok = true; break; } puts(ok? "YES" : "NO"); } return 0; }
四.
附原题:
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 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.
The first line contains a single integer tt (1≤t≤100; 1≤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,L (2≤n≤100; 2≤n≤100; 0≤U,R,D,L≤n).
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).
4 5 2 5 3 1 3 0 0 0 0 4 4 1 4 0 2 1 1 1 1
YES YES NO YES
Here are possible solutions to testcases 11, 22 and 44:

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人