#K83382. Valid Warehouse Configuration
Valid Warehouse Configuration
Valid Warehouse Configuration
You are given a warehouse represented by an ( n \times m ) grid. Each cell contains a non-negative integer, where a value of 0 represents an empty cell and any positive integer represents a crate. The configuration of the warehouse is considered valid if for each cell with a non-zero value, none of its adjacent cells (up, down, left, right) has the same value. In other words, for every cell ( (i, j) ) with ( grid[i][j] \neq 0 ), for every adjacent cell ( (i+\Delta i, j+\Delta j) ) with ( \Delta i, \Delta j \in { -1,0,1 } ) and (|\Delta i|+|\Delta j|=1), it must hold that ( grid[i+\Delta i][j+\Delta j] \neq grid[i][j] ).
If the warehouse configuration is valid, print the grid in the same format as the input. Otherwise, print -1.
inputFormat
The first line contains two integers ( n ) and ( m ) indicating the number of rows and columns respectively. The following ( n ) lines each contain ( m ) space-separated integers representing the grid.
outputFormat
If the warehouse configuration is valid, output the grid in ( n ) lines, each line containing ( m ) space-separated integers. Otherwise, output a single line with -1.## sample
3 3
1 2 3
2 0 4
3 4 1
1 2 3
2 0 4
3 4 1
</p>