#K51672. Minimum Effort Path on a Grid
Minimum Effort Path on a Grid
Minimum Effort Path on a Grid
You are given a 2D grid where each cell has an elevation value. Your task is to find the minimum effort path from the top-left corner to the bottom-right corner. The effort of a path is defined as the maximum absolute difference in elevations between two consecutive cells along the path. Formally, if a path is represented as a sequence of cells (p_1, p_2, \dots, p_k), the effort is
(E = \max_{1 \leq i < k} \left|e(p_{i+1}) - e(p_i)\right|).
You can only move in four directions: up, down, left, and right. The grid dimensions and the elevations are provided as input. Your solution should process multiple test cases efficiently and output the minimum effort for each test case.
inputFormat
The first line contains an integer (T) representing the number of test cases. For each test case, the first line contains two integers (N) and (M) (the number of rows and columns). The following (N) lines each contain (M) space-separated integers indicating the grid's elevation values. Input is read from standard input (stdin).
outputFormat
For each test case, output a single integer representing the minimum effort required to traverse from the top-left to the bottom-right cell, each on a new line. Output should be written to standard output (stdout).## sample
3
3 3
1 2 2
3 8 2
5 3 5
2 2
1 2
3 4
4 4
1 2 2 3
3 8 2 7
5 8 5 9
6 3 4 1
2
2
3
</p>