#C710. Ensuring Minimum Manhattan Distance
Ensuring Minimum Manhattan Distance
Ensuring Minimum Manhattan Distance
Given a grid of characters with 'B'
representing special markers and other cells containing digits, your task is to verify that every pair of 'B'
in the grid is at least a specified Manhattan distance d apart. The Manhattan distance between two cells at coordinates \((i_1,j_1)\) and \((i_2,j_2)\) is defined as \(|i_1-i_2|+|j_1-j_2|\). If any two 'B'
cells are closer than d
, the configuration is invalid and you must output "Impossible". Otherwise, output the grid in the same format (each row as a string).
The input starts with an integer t
specifying the number of test cases. For each test case, the first line contains three integers n
, m
, and d
— the number of rows, columns, and the required minimum Manhattan distance. This is followed by n
lines each consisting of a string that represents a row of the grid.
For each test case, if the grid satisfies the condition, output the grid with each row on a new line. Otherwise, output a single line with the word "Impossible".
inputFormat
The input is given via standard input and has the following format:
t n m d row1 row2 ... rown ... (repeated t times)
Here, t
is the number of test cases. For each test case, n
and m
denote the number of rows and columns of the grid, and d
is the required minimum Manhattan distance. Each of the following n
lines represents a row of the grid.
outputFormat
For each test case, output the result on standard output. If the grid satisfies the condition (i.e. every two 'B'
cells are at least a Manhattan distance of d
apart), then output n
lines representing the modified grid. If the condition is not satisfied, output a single line: Impossible
.
Note: For multiple test cases, outputs are printed consecutively in the order of input.
## sample3
3 3 2
B0B
000
B0B
4 5 3
B302B
00000
00000
B400B
3 3 2
BBB
000
BBB
B0B
000
B0B
B302B
00000
00000
B400B
Impossible
</p>