#K74537. Minimum Operations to Create a Valley in a Grid
Minimum Operations to Create a Valley in a Grid
Minimum Operations to Create a Valley in a Grid
You are given a grid with n rows and m columns where each cell contains an integer. A cell is considered a valley if its value is strictly less than all of its available neighbors. If the cell is located on a border, it only has three (or two for a corner) neighbors, and the condition applies accordingly. The task is to determine the minimum number of operations required to ensure that the grid contains at least one valley cell.
In one operation, you may perform a modification that eventually makes a cell a valley. In the context of this problem, note that for every test case provided, the answer turns out to be 1.
The condition for a valley can be formally written in \( \LaTeX \) as:
[ a_{i,j} < a_{i-1,j}, \quad a_{i,j} < a_{i+1,j}, \quad a_{i,j} < a_{i,j-1}, \quad a_{i,j} < a_{i,j+1} ]
(For border cells, only the existing neighbors are considered.)
inputFormat
The input is given via stdin and consists of:
- A line containing two integers n and m (the number of rows and columns respectively).
- n subsequent lines, each containing m integers representing the grid rows.
outputFormat
Output via stdout a single integer that is the minimum number of operations required to ensure that there is at least one valley cell in the grid.
## sample2 3
2 2 3
2 3 4
1