#P12529. [XJTUPC 2025] 对称隔离:黑白之战

    ID: 12362 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 3 上传者: 标签>模拟2025Special JudgeO2优化高校校赛

[XJTUPC 2025] 对称隔离:黑白之战

Description

You are playing a game called "Your Craft". In this game, you can freely create and destroy different types of blocks in a three-dimensional space filled with blocks.

Now, you have a grid composed of N×MN \times M cells beneath your feet. At the beginning, you covered these cells with white carpets, and then you went to sleep.

Unfortunately, while you were sleeping, the problem setter secretly replaced some of the white carpets with black carpets.

When you woke up and opened the game again, you found that your masterpiece had been tainted, and you were very angry! So you decided to make the best of the situation by keeping the existing black carpets unchanged and dyeing some of the white carpets black to satisfy the following two conditions:

  • No two cells with black carpets can share a common edge, which means:
    • If the cell in the ii-th row and jj-th column contains a black carpet, then the cells in the (i1)(i-1)-th row and jj-th column, (i+1)(i+1)-th row and jj-th column, ii-th row and (j1)(j-1)-th column, and ii-th row and (j+1)(j+1)-th column cannot contain black carpets if they have carpets.
  • The final pattern must have at least one horizontal or vertical line of symmetry, which means at least one of the following two propositions must be satisfied:
    • For any i[1,N]i \in [1,N] and j[1,M]j \in [1,M], the color of the carpet in the ii-th row and jj-th column must be the same as the color of the carpet in the (Ni+1)(N-i+1)-th row and jj-th column;
    • For any i[1,N]i \in [1,N] and j[1,M]j \in [1,M], the color of the carpet in the ii-th row and jj-th column must be the same as the color of the carpet in the ii-th row and (Mj+1)(M-j+1)-th column.

You need to determine whether you can achieve these two conditions.

Note: You cannot destroy the existing black carpets.

Input Format

The first line contains two integers NN and MM (1N,M1001\le N,M\le 100), separated by a space, representing the size of the grid.

The next NN lines each contain a string SS of length MM. In the string SS, a character W\tt{W} indicates that this position has a white carpet, while a character B\tt{B} indicates that this position has a black carpet. No other characters will appear in the string.

Output Format

If you can satisfy the conditions given in the problem, output the string Yes\tt{Yes}; otherwise, output the string No\tt{No}.

The answer is case insensitive. For example, the strings yEs\tt{yEs}, Yes\tt{Yes}, yes\tt{yes}, and YES\tt{YES} will all be recognized as if you can satisfy the conditions.

4 4
WWWW
WBWW
WWWW
WWWW

No
4 4
BWWB
WWWW
WWWW
WWWB

Yes
4 4
BWWW
WBWW
WWBW
WWWW

No
5 5
BWBWB
WBWBW
BWBWB
WBWBW
BWBWB

Yes
2 2
BB
WW

No
2 2
WW
WW

Yes
8 6
WWBWWW
BWWWWW
WBWWWW
WWWWBW
BWWWWW
WWBWWW
WBWWWW
BWWWWW
No

Hint

For the first sample:

The leftmost image shows the situation you saw when you woke up. You cannot achieve the conditions by changing white carpets to black carpets. The pictures in the middle and on the rightmost show two possible ways to replace the carpet, but they cannot satisfy the two requirements in the question (the middle image does not have a horizontal or vertical line of symmetry; in the rightmost image, the black carpets share a common edge). It can be proven that any way of changing the carpets does not satisfy the requirements, so output No\tt{No}.

For the second sample:

The image shows a valid way to change the carpets. You can change the white carpet in the lower left corner to black, which satisfies both requirements in the problem. Therefore, you should output Yes\tt{Yes}.

For the third sample:

This is one possible way to change the carpets, but it is not a valid one. Remember: only configurations with a horizontal or vertical line of symmetry are considered valid.

For the fourth sample:

The situation you saw when you woke up is already valid, so you do not need to make any changes.

For the fifth sample: When you woke up, there were already two black carpets sharing a common edge, so you cannot change the white carpets to meet the requirements.

For the sixth sample: A pattern with all white carpets is also considered valid because it has a horizontal or vertical line of symmetry and does not have two black carpets sharing a common edge.