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 N×NN \times N grid. The cell in row ii and column jj has coordinates (i,j)(i,j). 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 11 cell, make a right turn, then move for at least 11 cell, make a right turn, then move for at least 11 cell, make a right turn, and then move for at least 11 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 NN and KK, representing the size of the park and the number of fountains.

The next KK lines each contain two integers xx and yy, indicating that there is a fountain at coordinates (x,y)(x,y).

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.

2N10002\le N\le 1000, 0Kmin(2000,N2)0\le K\le \min(2000,N^2), 1x,yN1\le x,y\le N.

Sample Explanation:

The park is shown in the figure. The rightmost picture shows the longest spiral path, whose length is 1414, and there is no longer path.

Note: The first two pictures both show valid spiral paths to help understanding.

Translated by ChatGPT 5