#P16439. [XJTUPC 2026] 鲜艳 / 方格
[XJTUPC 2026] 鲜艳 / 方格
Problem Description
Y2hlOTYw is a chess lover. However, he does not have his own chessboard.
One day, he got a piece of black-and-white grid paper with rows and columns. Of course, this is not a standard chessboard, but Y2hlOTYw does not care. He thinks that if every connected component formed by cells of the same color via 4-directional adjacency (up, down, left, right) has a rectangular shape, then this grid paper can be used as a chessboard.
Now Y2hlOTYw wants you to help determine whether this grid paper can be used as a chessboard.
Formally, let the grid paper be , where and are positive integers. Each cell is assigned a color $\text{color}(i,j) \in \{\text{black},\text{white}\}$.
Two cells and are adjacent if and only if .
For a fixed color , consider the subset . Define an equivalence relation on : two cells are equivalent if and only if there exists a sequence of cells such that every cell belongs to , and for any (), cell is adjacent to cell . Each equivalence class is called a connected component. A connected component is maximal, i.e., it cannot be expanded by adding any adjacent cell of the same color.
The shape of a set of cells is called a rectangle if there exist integers and such that:
$$R = \{(i,j) \mid r_1 \le i \le r_2,\ c_1 \le j \le c_2\}$$Now you need to determine whether the shape of every connected component is a rectangle.
Input Format
This problem contains multiple test cases. The first line of the input contains a positive integer (), indicating the number of test cases.
Next are the descriptions of the test cases.
The first line of each test case contains two integers and (), separated by a space, indicating that the grid paper has rows and columns.
The next lines: the -th line contains a string of length exactly , describing the given grid paper. It is guaranteed that contains only characters and . For any integers and ():
- If the -th character of the -th line is , then the cell in row and column is black ().
- If the -th character of the -th line is , then the cell in row and column is white ().
It is guaranteed that the sum of over all test cases does not exceed .
Output Format
For each test case, output one line containing a string:
- If this grid paper can be used as a chessboard, i.e., the shape of every connected component is a rectangle, output .
- Otherwise, output .
The answer is case-insensitive. For example, , , , and will all be considered as .
4
2 3
110
110
4 3
110
111
111
111
5 5
00000
01110
01010
01110
00000
8 8
01010101
10101010
01010101
10101010
01010101
10101010
01010101
10101010
Yes
No
No
Yes
Hint
Translated by ChatGPT 5