#P2239. Spiral Matrix Query
Spiral Matrix Query
Spiral Matrix Query
A spiral matrix of size \(n \times n\) is generated by the following procedure:
Start at the top-left corner of the matrix (cell at row 1, column 1) and initially move to the right. At each step, if the cell in front has not been visited, continue moving in the same direction; otherwise, turn right. Repeat this process until all cells have been visited. Each visited cell is filled in order with the numbers \(1, 2, 3, \dots, n^2\).
For example, when \(n = 4\), the spiral matrix is:
$$ \begin{pmatrix} 1 & 2 & 3 & 4 \\ 12 & 13 & 14 & 5 \\ 11 & 16 & 15 & 6 \\ 10 & 9 & 8 & 7 \end{pmatrix} $$Given the size of the matrix \(n\) along with two integers \(i\) and \(j\), your task is to determine the number contained in the cell at row \(i\) and column \(j\) of the spiral matrix.
inputFormat
The input consists of three space-separated integers: \(n\), \(i\), and \(j\). \(n\) is the size of the matrix, and \(i\) and \(j\) indicate the row and column respectively (with 1-based indexing).
Constraints: It is guaranteed that \(1 \leq i, j \leq n\).
outputFormat
Output a single integer representing the value in the cell at the \(i\)th row and \(j\)th column of the spiral matrix.
sample
4 3 2
16