Cross Country Skiing
Time limit1sMemory limit512 MB
Find the smallest elevation gap D that keeps every waypoint mutually reachable through adjacent cells.
- Level
Medium5 of 10
- Topics
- Minimum spanning tree, Union-find, Sorting
- Solved
- No attempts yet
Problem
The cross-country skiing course at the winter Moolympics is described by an grid of elevations (). Every elevation is an integer between and .
Some cells of the grid are marked as waypoints. The organizers want to give the whole course a difficulty rating so that a cow can get from any waypoint to any other waypoint by repeatedly skiing from a cell to an adjacent cell whose elevation differs by at most in absolute value. Two cells are adjacent when one is directly north, south, east, or west of the other. The difficulty rating of the course is the smallest for which all waypoints are mutually reachable.
If the course has fewer than two waypoints, the difficulty rating is .
Input
- Line 1: the integers and .
- Lines 2 to : each line has integer elevations.
- Lines to : each line has values, each either or . A marks a waypoint.
Output
- Line 1: the difficulty rating of the course, the smallest that keeps every waypoint reachable from every other waypoint.
Hint
The first example describes a grid. The upper-left, upper-right, and lower-right cells are waypoints.
With the three waypoints reach each other. With any smaller than , the upper-right waypoint cannot be reached from the other two.