#P12058. Painting Grid with Limited Consecutive Colors
Painting Grid with Limited Consecutive Colors
Painting Grid with Limited Consecutive Colors
Given positive integers ( n ) and ( k ), you are to color an ( n\times n ) grid such that exactly ( kn ) cells are colored black and the remaining cells white. The coloring must satisfy the following conditions:
- In every row and column, there must not be three consecutive cells that are all of the same color. In other words:
- There is no ( 1\le i\le n,, 1\le j\le n-2 ) with the cells at ( (i,j),,(i,j+1),,(i,j+2) ) all black or all white.
- There is no ( 1\le j\le n,, 1\le i\le n-2 ) with the cells at ( (i,j),,(i+1,j),,(i+2,j) ) all black or all white. - Each column must contain exactly ( k ) black cells.
- Let the black cell row indices in the ( j )-th column be ( x_{1,j} < x_{2,j} <\cdots< x_{k,j} ). Then for every ( 1\le j<n ) and for every ( 1\le i\le k ), we require that ( |x_{i,j} - x_{i,j+1}|\le 1 ).
Output any valid grid meeting these requirements, or output -1 if no solution exists. If a solution exists, the grid should be printed as ( n ) lines with ( n ) characters each, where each character is either 'B' (black) or 'W' (white).
inputFormat
The input consists of a single line containing two space-separated integers ( n ) and ( k ).
outputFormat
If a valid coloring exists, output ( n ) lines, each containing a string of length ( n ) representing the grid (with characters 'B' for black and 'W' for white). Otherwise, output -1.
sample
3 1
BWW
WBW
WWB
</p>