#BS0000438. Ngày cuối cùng có thể vượt qua vùng đất (Last Day Where You Can Still Cross)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Ngày cuối cùng có thể vượt qua vùng đất (Last Day Where You Can Still Cross)
Last Day Where You Can Still Cross
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
A rectangular region consists of rows and columns, numbered from to and from to , respectively. On day , all cells are dry land.
Every day, exactly one previously dry cell becomes flooded. The cell flooded on day has coordinates and remains flooded on every subsequent day. You are given the flooding order of all cells.
After the cell scheduled for a particular day has flooded, you wish to walk from the first row to the last row. You may start at any dry cell in row and finish at any dry cell in row . Each move must go to a dry cell sharing a side with the current cell, in one of the four directions. You cannot leave the region or enter a flooded cell.
Find the latest day on which such a route still exists.
Input
- The first line contains two integers , the numbers of rows and columns.
- The next lines each contain two integers; line gives coordinates of the cell flooded on day .
Output
Print one integer: the latest day on which walking from row to row is still possible.
Subtasks
- Subtask 1 (20 points): ; ; , for all ; coordinate pairs are pairwise distinct.
- Subtask 2 (30 points): ; ; , for all ; coordinate pairs are pairwise distinct.
- Subtask 3 (50 points): ; ; , for all ; coordinate pairs are pairwise distinct.
Examples
Example 1
Input
2 2
1 1
2 1
1 2
2 2
Output
2
Explanation
On day , floods; on day , floods. Both cells in column are still dry, giving a vertical crossing. On day , floods too, leaving no land in the top row. The last possible day is .

Example 2
Input
2 2
1 1
1 2
2 1
2 2
Output
1
Explanation
On day , floods but column remains dry, so can reach . On day , floods, eliminating all starting cells. Thus the answer is .
Example 3
Input
3 3
1 2
2 1
3 3
2 2
1 1
1 3
2 3
3 2
3 1
Output
3
Explanation
After day , are flooded. The route remains dry. On day , also floods: is isolated, and the route from stops at without reaching the bottom. Hence day is the last possible day.