#BS0000430. Vượt qua hẻm núi (Canyon Crossing)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Vượt qua hẻm núi (Canyon Crossing)
Canyon Crossing
Source: Kattis
Version: Phuoc Hung OJ Extended
Problem Statement
A new route through a canyon is under construction when a rising river floods the low-lying terrain. To reduce the impact of flooding, you want to maximize the lowest terrain height along the route. You have purchased rope bridges to bypass low cells.
The map is a rectangular grid with rows and columns. Cell has height . Rows are numbered from to from north to south, and columns from to .
A route starts at any cell in row and finishes at any cell in row . Each move must go to a cell sharing an edge with the current cell; diagonal moves are not allowed.
Each bridge allows exactly one cell to be crossed without including that cell's height in the minimum height of the route. Bridges may be used at the first cell, the last cell, or consecutive cells. At most bridges may be used.
The value of a route is the minimum height of its cells not bypassed by bridges. Since and every route visits at least cells, at least one cell contributes to this value.
Find the greatest possible route value.
Input
- The first line contains three integers : the number of rows, the number of columns, and the available number of bridges.
- The next lines each contain integers. The -th integer of the -th such line is , the corresponding cell height.
Output
Print one integer: the maximum value of a valid route.
Subtasks
- Subtask 1 (20 points): ; ; for every , .
- Subtask 2 (30 points): ; ; for every , .
- Subtask 3 (50 points): ; ; for every , .
Examples
Example 1
Input
5 3 1
1 1 3
3 3 3
0 0 0
2 2 1
1 2 1
Output
2
Explanation
Take and bridge . The remaining heights are , so the route value is . A value of would require a bridge in each of rows and , since all their cells are below . One bridge is insufficient.

Example 2
Input
5 3 3
2 1 1
2 1 1
1 1 1
1 1 2
1 1 2
Output
2
Explanation
Go from to , then . Bridge . The four remaining cells all have height . No cell is higher than , so no larger value is possible.

Example 3
Input
3 2 2
1 1
4 4
1 2
Output
4
Explanation
Follow column from through to . Bridge the first and last cells, of heights and . The only remaining height is , which is also the maximum grid height. Thus the answer is .
Adapted from Canyon Crossing, © 2019 The BAPC 2019 jury. Licensed under CC BY-SA 4.0. Statements adapted and sample illustrations redrawn.