#P11476. Matrix Painting
Matrix Painting
Matrix Painting
You are given an \(n \times n\) matrix initially filled with white cells (represented by the character W
). You are allowed to perform the following operations any number of times:
- Choose a row or a column and overwrite every cell in that row or column with a single color: red (
R
) or blue (B
).
The final configuration (target state) of the matrix is given. Your task is to output a sequence of operations which, when applied in order, transform the initial white matrix into the given target state, or report that no solution exists.
Important: You do not need to minimize the number of operations; any sequence of valid operations that achieves the target state will be accepted.
Note on the process: Suppose that a row operation on row \(i\) is performed at time \(t_r\) and a column operation on column \(j\) is performed at time \(t_c\). The final color of cell \((i,j)\) will be determined by the operation that comes later (i.e. if \(t_r > t_c\) then the cell takes the row’s color, and if \(t_c > t_r\) then it takes the column’s color). No operation produces white; initially every cell is white, so any cell that remains white in the final configuration must never be overwritten by any operation.
A key observation for a solution is as follows. Partition the rows into two groups:
- Painted rows: These are rows in which a row operation is performed after any column operations. Such a row becomes uniformly colored with the chosen color.
- Non-painted rows: For these rows no row operation is applied after column operations. Therefore, for each column you may rely on a column operation (if applied) to dictate the final color, or the cell remains white if no column operation is ever applied in that column.
Thus, if you denote by \(S\) the set of painted rows and by \(T\) the set of columns in which a column operation is applied, then the target matrix must have the following form:
- For every row \(i \in S\), all cells must have the same color (either
R
orB
), because the row operation overwrites the entire row. - For every row \(i \notin S\), the final color in column \(j\) will be:
Cj
if a column operation is applied in column \(j\) (and such an operation must use the same color for all rows),W
otherwise.
Your task is to decide which rows should be painted (via a row operation) and, for the remaining (non-painted) rows, determine the color (if any) for each column such that the above consistency condition holds. Then output a sequence of operations in which all column operations are performed first, followed by the row operations (so that a row operation overrides any previous column operations in that row).
inputFormat
The input begins with an integer \(n\) (\(1 \le n \le 1000\)), the size of the matrix. The following \(n\) lines each contain a string of length \(n\) consisting of characters W
(white), R
(red) or B
(blue), representing the target state of the matrix.
outputFormat
If a valid sequence of operations exists, first output an integer \(k\) (the number of operations). Then output \(k\) lines describing the operations. Each operation is formatted as either row i C
or col j C
, where i
(or j
) is a 1-indexed row (or column) number and C
is either R
or B
.
If it is impossible to achieve the target state using the allowed operations, output a single line containing -1
.
sample
3
RRR
WBW
WBW
2
col 2 B
row 1 R
</p>