Problem 274
Path With Minimum Effort
Medium · 20 pts · Matrix, Shortest Path, Binary Search, Heap
Problem
You are given a grid of heights. You start in the top-left cell and want to reach the bottom-right cell, moving up, down, left or right one cell at a time. The effort of a route is the largest absolute height difference between two consecutive cells on it, not the total. Find the minimum possible effort over all routes. Run a Dijkstra-style search where the cost of a path is its maximum step instead of its sum. Alternatively, binary search on the effort and check with BFS whether the corner can be reached using only steps up to that limit.
Input
First line: R and C. Next R lines: C heights.
Output
Print the minimum effort.
Constraints
1 <= R, C <= 100; 1 <= height <= 1000000
Example input
3 3 1 2 2 3 8 2 5 3 5
Example output
2
Explanation
The route 1 → 3 → 5 → 3 → 5, going down the left column and along the bottom row, changes height by 2, 2, 2 and 2. Its effort is 2, and every route has some step of at least 2, so the answer is 2.