英文题目
Description
Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black or white).
Leo has a magical brush which can paint any row with black color, or any column with white color. Each time he uses the brush, the previous color of cells will be covered by the new color. Since the magic of the brush is limited, each row and each column can only be painted at most once. The cells were painted in some other color (neither black nor white) initially.
Please write a program to find out the way to paint the grid.
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
The first line contains an integer N (1 <= N <= 500). Then N lines follow. Each line contains a string with N characters. Each character is either ‘X’ (black) or ‘O’ (white) indicates the color of the cells should be painted to, after Leo finished his painting.
Output
For each test case, output “No solution” if it is impossible to find a way to paint the grid.
Otherwise, output the solution with minimum number of painting operations. Each operation is either “R#” (paint in a row) or “C#” (paint in a column), “#” is the index (1-based) of the row/column. Use exactly one space to separate each operation.
Among all possible solutions, you should choose the lexicographically smallest one. A solution X is lexicographically smaller than Y if there exists an integer k, the first k - 1 operations of X and Y are the same. The k-th operation of X is smaller than the k-th in Y. The operation in a column is always smaller than the operation in a row. If two operations have the same type, the one with smaller index of row/column is the lexicographically smaller one.
Sample Input
2
2
XX
OX
2
XO
OX
Sample Output
R2 C1 R1
No solution
翻译(偷来的)
先有个 n × n n \times n n×n 的矩阵,你需要通过染色刷子把矩阵中的元素染色特定的颜色。
有两种染色刷子,行刷子和列刷子。行刷可以把一行刷成黑色,列刷子可以把一列刷成白色。
一行或者一列最多刷一次。
求最少刷几次,就可以变成目标矩阵(目标矩阵中,O代表白色,X代表黑色)。
思路
题目看上去无从下手。
但是我们仔细想一想,最后一次操作,肯定是把一行变黑,或把一列变白。
换言之,棋盘的每一种状态都是可以找到它的上一个状态的。
是不是有点感觉了?(反正我啥都没想到)
单独考虑每行每列,如果第 i i i 行中既出现了白色又出现了黑色,那么说明在 R i R_i Ri 操作后才会出现 C i C_i Ci 操作,注意如果全是白色就不需要 R i R_i Ri 操作了。
列也同样这么考虑。
这样我们就能知道操作之间的相对顺序。
还不明显吗?不明显,只需拓扑排序即可。
把每个行操作、列操作 R i , C i R_i,C_i R