luogu#P8064. [BalkanOI 2012] Spiral
[BalkanOI 2012] Spiral
Background
On a whim, you go to the park to ride a bike.
Problem Description
The park can be viewed as an grid. The cell in row and column has coordinates . Each cell is either a passable road or an impassable fountain.
You want to ride along a spiral path.
- A spiral path is defined as: starting from a cell, move in one direction for at least cell, make a right turn, then move for at least cell, make a right turn, then move for at least cell, make a right turn, and then move for at least cell (turn right exactly three times).
- A spiral path must not pass through any fountain.
Now you want to find the longest spiral path you can ride (the path length is the number of visited cells, including the start and end cells).
It is guaranteed that at least one spiral path exists.
A spiral path must not visit the same cell more than once.
Input Format
The first line contains two integers and , representing the size of the park and the number of fountains.
The next lines each contain two integers and , indicating that there is a fountain at coordinates .
Output Format
Output one integer: the longest route you can ride (i.e., the longest spiral path).
6 9
2 1
2 4
4 4
6 2
6 3
5 3
4 6
5 1
2 6
14
Hint
Constraints:
Subtask#0 is the sample.
, , .
Sample Explanation:
The park is shown in the figure. The rightmost picture shows the longest spiral path, whose length is , and there is no longer path.

Note: The first two pictures both show valid spiral paths to help understanding.
Translated by ChatGPT 5