#BS0000435. Bơi trong nước dâng (Swim in Rising Water)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Bơi trong nước dâng (Swim in Rising Water)
Swim in Rising Water
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
A square terrain consists of rows and columns, numbered from to . Cell has elevation .
Rain causes the water level to rise. At time (), the water level equals . A cell is available for swimming when its elevation does not exceed the water level.
You start at cell and wish to reach cell . A move is allowed only between cells sharing a side (up, down, left or right) within the terrain. Both cells must have elevations at most . Swimming between cells takes zero time.
Find the earliest time at which you can swim from the starting cell to the destination.
Input
- The first line contains an integer , the size of the terrain.
- The next lines each contain integers; line lists elevations of row .
Output
Print one integer: the earliest time at which cell can be reached.
Subtasks
- Subtask 1 (20 points): ; for all ; all elevations are pairwise distinct.
- Subtask 2 (30 points): ; for all ; all elevations are pairwise distinct.
- Subtask 3 (50 points): ; for all ; all elevations are pairwise distinct.
Examples
Input
2
0 2
1 3
Output
3
Explanation
The destination has elevation , so it cannot be reached for . At , the cells form a route with elevations . The earliest time is therefore .

Input
5
0 1 2 3 4
24 23 22 21 5
12 13 14 15 16
11 17 18 19 20
10 9 8 7 6
Output
16
Explanation
At , move along the first row to column , down to row , left to column , down to the last row, and right to the destination. The highest elevation on this route is . For , the only usable cell in row is in column , and the cell immediately below it has elevation . The destination is unreachable.
