#P6809. Matrix Reconstruction from Neighbour Counts
Matrix Reconstruction from Neighbour Counts
Matrix Reconstruction from Neighbour Counts
You are given an integer matrix (A) of size (H \times W). Each element (A_{i,j}) is defined as the sum of indicators for character 'X' in a corresponding binary matrix (B) (of the same size) in all cells that are adjacent to or coincident with ((i,j)). In other words, if we define (B_{i,j}\nolinebreak\in {0,1}) (where 1 represents 'X' and 0 represents '.'), then for every valid (i,j) we have
[
A_{i,j} = \sum_{p = \max(0, i-1)}^{\min(H-1, i+1)} \sum_{q = \max(0, j-1)}^{\min(W-1, j+1)} B_{p,q}.
]
Your task is to construct and output any matrix (B) of size (H \times W) composed only of the characters .
and X
such that the equation above holds for every cell. It is guaranteed that the input matrix (A) is derived from at least one valid matrix (B).
inputFormat
The input begins with two integers (H) and (W) (the numbers of rows and columns). This is followed by (H) lines, each containing (W) space‐separated integers, representing the matrix (A).
outputFormat
Output (H) lines, each with a string of length (W) consisting only of characters .
and X
. The output matrix must be a valid matrix (B) such that for each cell ((i,j)) the sum of 'X's in all allowed neighbouring positions (including the cell ((i,j)) itself) is exactly (A_{i,j}).
sample
1 1
0
.
</p>