#K79557. Minimizing Maximum Height Difference Path

    ID: 35334 Type: Default 1000ms 256MiB

Minimizing Maximum Height Difference Path

Minimizing Maximum Height Difference Path

You are given an N x M grid where each cell contains an integer representing the height at that position. Your task is to find a path from the top-left corner (0, 0) to the bottom-right corner (N-1, M-1) such that the maximum difference between the heights of any two adjacent cells along the path is minimized.

Two cells are considered adjacent if they share an edge. The height difference between two adjacent cells with heights \( a \) and \( b \) is defined as \( |a-b| \). You need to choose a path that minimizes the maximum of all such differences encountered along the path.

Input/Output: The input is read from stdin and the output is written to stdout.

inputFormat

The first line of input contains two integers N and M, representing the number of rows and columns in the grid.

Each of the following N lines contains M space-separated integers, which represent the heights of the grid cells.

outputFormat

Output a single integer, which is the minimized maximum height difference encountered along the optimal path from (0, 0) to (N-1, M-1).

## sample
3 3
1 3 5
2 8 3
4 7 6
3

</p>