Ski Course Rating
Time limit1sMemory limit128 MB
From each marked start cell, find the smallest elevation-step limit that connects it to at least T cells.
- Level
Medium7 of 10
- Topics
- Union-find, Sorting, Graph
- Solved
- No attempts yet
Problem
The cross-country skiing course at the winter Moolympics is given as an grid of elevations, with . Every elevation is an integer between and .
Some cells of the grid are marked as starting points of the course. The organizers want to give each starting point a difficulty rating. The difficulty rating of a starting point is the smallest such that a cow who starts at reaches at least cells in total, counting itself, when she may only step between adjacent cells whose elevations differ by at most . Here . Two cells are adjacent when one lies directly north, south, east, or west of the other.
Compute the difficulty rating of every starting point and help the organizers.
Input
- Line 1: the integers , , and .
- Lines to : each line holds integer elevations.
- Lines to : each line holds values, each of them or . A marks a cell that is a starting point.
Output
- Line 1: the sum of the difficulty ratings of all starting points. A single rating fits in a 32-bit integer, but the sum may not.
Hint
Input details
The ski course is a grid of elevations. The upper-left cell and the lower-right cell are starting points, and from each starting point the cow must reach at least cells.
Output details
The difficulty rating of the upper-left starting point is , and the rating of the lower-right starting point is .