CF2161B.Make Connected
普及+/提高
通过率:0%
时间限制:1.50s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given an n×n grid where some cells are colored black, and the rest are white. You may paint some of the white cells black to achieve the following conditions:
- There is at least one black cell.
- All black cells must be orthogonally connected, that is, it should be possible to go from any black cell to any other by crossing several vertical or horizontal cell borders while visiting only black cells. You can't go directly through the corner of a cell.
- There are no three consecutive black cells aligned vertically or horizontally.
You can't paint black cells white.
Determine whether it is possible to paint some white cells black in order to satisfy all the conditions.
给你一个 n×n 的网格,其中部分格子被涂成黑色,其余为白色。你可以将一些白色格子涂成黑色,以满足以下条件:
- 至少存在一个黑色格子;
- 所有黑色格子必须正交连通,即:任意两个黑色格子之间都可通过仅经过黑色格子、且每次仅跨越垂直或水平相邻格子边界的路径相互到达(不允许直接穿过格子的角点);
- 不存在三个连续的黑色格子在同一条竖直方向或水平方向上对齐。
你不能将已有的黑色格子涂成白色。
判断是否可以通过将若干白色格子涂成黑色,使得所有条件均被满足。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤1000). The description of the test cases follows.
The first line contains an integer n (1≤n≤100) — the size of the grid.
The following n lines contain n characters each — the grid description, where each character represents a cell:
- . — a white cell;
-
— a black cell.
It is guaranteed that the sum of n over all test cases does not exceed 2000.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤1000)。随后是各测试用例的描述。
第一行包含一个整数 n(1≤n≤100)—— 网格的大小。
接下来的 n 行,每行包含 n 个字符 —— 描述该网格,其中每个字符代表一个单元格:
.— 白色单元格;#— 黑色单元格。
保证所有测试用例的 n 值之和不超过 2000。
输出格式
For each test case, print "YES" if it is possible to paint some white cells black to satisfy all the conditions, and "NO" otherwise.
You may print each letter in any case (uppercase or lowercase). For example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as a positive answer.
对于每个测试用例,如果能够将某些白色格子涂成黑色以满足所有条件,则输出 "YES";否则输出 "NO"。
你可以以任意大小写形式输出每个字母(大写或小写)。例如,字符串 "yEs"、"yes"、"Yes" 和 "YES" 均会被识别为肯定回答。
输入输出样例
输入#1
11 1 . 1 # 3 .## .## ... 3 #.. .#. ..# 3 ### ... ... 3 #.# ... .#. 4 #### #..# #..# #### 3 ..# ... .#. 3 ..# #.. ... 5 #.#.# .#.#. #.#.# .#.#. #.#.# 5 ...#. ...#. ..... ##... .....
输出#1
YES YES YES YES NO NO NO YES YES NO YES
说明/提示
In the first test case, there are no black cells, so we must paint one cell black.
In the second and third test cases, the grid satisfies all the conditions from the very beginning.
In the fourth test case, one of the possible solutions is:
##.
.##
..#
In the fifth test case, the grid violates the "No three consecutive black cells should be aligned vertically or horizontally" condition from the very beginning, so there is no solution.
In the sixth test case, it can be shown that it is impossible to achieve a connected grid without violating the condition about three consecutive black cells.
在第一个测试用例中,没有黑色格子,因此我们必须将一个格子涂黑。
在第二个和第三个测试用例中,网格从一开始便满足所有条件。
在第四个测试用例中,一种可能的解是:
##.
.##
..#
在第五个测试用例中,网格从一开始便违反了“不能存在三个连续的黑色格子在水平或竖直方向上对齐”的条件,因此无解。
在第六个测试用例中,可以证明:若不违反“三个连续黑色格子”的限制,则无法使网格连通。
输入解题思路,AI测评打分。不知道怎么写?