#FT0000144. Quân xe trên lưới (Rook on Grid)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Quân xe trên lưới (Rook on Grid)
Rook on Grid
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
Consider an grid, with rows numbered top-to-bottom and columns left-to-right. There are distinct blocked cells, and is guaranteed to be empty.
A rook starts at . In one move, it may travel right or down through one or more consecutive unblocked cells along its current row or column; it cannot jump over or land on an obstacle. Moving left or up is forbidden.
Count the cells reachable in at most two moves, including the starting cell. Count each cell only once, even if there are several paths.
Input
The first line contains . The next lines each contain one obstacle location .
Output
Print the number of cells reachable in at most two moves.
Subtasks
- Subtask 1 (20 points): ; ; ; ; ; các vị trí chướng ngại đôi một khác nhau.
- Subtask 2 (30 points): ; ; ; ; ; các vị trí chướng ngại đôi một khác nhau.
- Subtask 3 (50 points): ; ; ; ; ; các vị trí chướng ngại đôi một khác nhau.
Examples
Example 1
Input
4 3 2
2 2
3 3
Output
10
Explanation
The grid has cells; exactly and are blocked. All other cells can be reached by a right-then-down or down-then-right route of at most two moves.
Example 2
Input
5 4 4
3 2
3 4
4 2
5 2
Output
14
Explanation
Of the cells, are blocked. The two additional free cells and cannot be reached in at most two allowed moves. Therefore the answer is .