#P7886. Connected K-Block Coloring of a Grid
Connected K-Block Coloring of a Grid
Connected K-Block Coloring of a Grid
Given positive integers \(n\), \(m\), and \(k\), determine whether it is possible to color an \(n \times m\) grid so that each color appears in exactly one connected component and each connected component has exactly \(k\) cells. If a solution exists, output one valid configuration.
A connected component means that for any two cells of the same color, there exists a path (moving only up, down, left, or right) from one cell to the other entirely consisting of cells of that color.
Note: It is necessary that \(k\) divides \(n \times m\), otherwise no valid partition exists.
inputFormat
The first and only line of input contains three positive integers \(n\), \(m\), and \(k\) separated by spaces.
outputFormat
If it is impossible to partition the grid under the given conditions, output a single line containing NO
(without quotes). Otherwise, output YES
on the first line, followed by \(n\) lines, each containing a string of length \(m\). The \(i\)-th line represents the colors of the cells in the \(i\)-th row. Each connected component (of size exactly \(k\)) must be labeled with a unique color. In the output, colors can be represented by letters (for example, using lowercase letters for the first 26 blocks and uppercase if needed).
sample
3 3 3
YES
aaa
bbb
ccc
</p>