#P2239. [NOIP 2014 普及组] 螺旋矩阵

[NOIP 2014 普及组] 螺旋矩阵

Description

A spiral matrix with nn rows and nn columns can be generated as follows:

Starting from the top-left corner (row 11, column 11), initially move to the right. If the cell ahead has not been visited, keep moving forward; otherwise, turn right. Repeat this process until all cells in the matrix have been visited. According to the visiting order, fill the cells with 1,2,3,,n21, 2, 3, \dots, n^2 in sequence to form a spiral matrix.

Below is the spiral matrix when n=4n = 4.

$$\begin{pmatrix} 1 & 2 & 3 & 4 \\ 12 & 13 & 14 & 5 \\ 11 & 16 & 15 & 6 \\ 10 & 9 & 8 & 7 \\ \end{pmatrix}$$

Given the matrix size nn and ii and jj, please compute the number at row ii, column jj in this matrix.

Input Format

A single line containing three integers nn, ii, jj, separated by a single space. They represent the matrix size, and the row and column of the number to query.

Output Format

A single integer, the number at row ii, column jj in the corresponding matrix.

4 2 3
14

Hint

Constraints

  • For 50%50\% of the testdata, 1n1001 \leqslant n \leqslant 100.
  • For 100%100\% of the testdata, 1n30,0001 \leqslant n \leqslant 30{,}000; 1in1 \leqslant i \leqslant n; 1jn1 \leqslant j \leqslant n.

Translated by ChatGPT 5