#P7141. Non-Adjacent Chessboard Pieces
Non-Adjacent Chessboard Pieces
Non-Adjacent Chessboard Pieces
You are given a chessboard with \(n\) rows and \(m\) columns (i.e. a total of \(n\times m\) cells). You need to place at most one piece in each cell so that for every column \(1\le i\le m\) the number of pieces in the \(i\)th column is exactly \(a_i\). In addition, any two pieces on the board must not be adjacent via an edge (i.e. they must not share a common side).
Your task is to determine if there exists a valid configuration meeting these conditions. If one exists, output any valid configuration. Otherwise, output NO
.
Note: Two cells are considered adjacent if they share a common edge (vertically or horizontally adjacent). In each column, since pieces in adjacent rows would be vertically adjacent, the pieces placed in the column must not be in consecutive rows. Moreover, if a piece is placed in row \(r\) of column \(i\), then column \(i+1\) should not have a piece in row \(r\) (to avoid horizontal adjacency).
inputFormat
The first line contains two integers \(n\) and \(m\) (the number of rows and columns respectively).
The second line contains \(m\) integers \(a_1, a_2, \ldots, a_m\), where \(a_i\) is the required number of pieces in the \(i\)th column.
outputFormat
If a valid configuration exists, first output a line containing YES
. Then output \(n\) lines, each containing a string of length \(m\), representing the chessboard. In the output board, use *
to denote a cell with a piece and .
to denote an empty cell. The first printed row corresponds to the top row of the chessboard.
If no valid configuration exists, output a single line containing NO
.
sample
3 3
1 1 1
YES
.
.*.
...
</p>