#K4311. Adjacent Mines Counter
Adjacent Mines Counter
Adjacent Mines Counter
You are given a grid of size \(m \times n\) where each cell contains either \(0\) (no mine) or \(1\) (a mine). Your task is to compute a resulting grid of the same dimensions where:
- If a cell contains a mine (i.e. its value is 1), then the corresponding cell in the result should be marked with
9
. - If a cell does not contain a mine (its value is 0), then replace it with the number of mines present in its 8 neighboring cells. The neighbors are defined by the set \(\{(-1,-1), (-1,0), (-1,1), (0,-1), (0,1), (1,-1), (1,0), (1,1)\}\).
This problem is similar in spirit to the Minesweeper game where you count the adjacent mines. Make sure your solution reads from stdin
and writes to stdout
.
inputFormat
The input is given via standard input (stdin
) with the following format:
m n row1 row2 ... row_m
Here, the first line contains two space-separated integers, \(m\) and \(n\), representing the number of rows and columns, respectively. The next \(m\) lines each contain \(n\) space-separated integers (either 0 or 1) representing the grid.
outputFormat
The output should be the resulting grid printed to standard output (stdout
) in the following format:
result_row1 result_row2 ... result_row_m
Each row of the result is printed on a new line with values separated by a single space.
## sample3 3
1 0 1
0 1 0
1 0 1
9 3 9
3 9 3
9 3 9
</p>