#C6620. Rook Moves on a Spiral Grid
Rook Moves on a Spiral Grid
Rook Moves on a Spiral Grid
In this problem, you are given a square grid of size \(n \times n\) that is filled with integers from 1 to \(n^2\) in a clockwise spiral order, starting from the top-left corner.
You are also given several queries. Each query consists of two numbers. The task is to determine if a chess rook can move directly between the two positions corresponding to these numbers on the grid. In chess, a rook can move any number of squares horizontally or vertically. Therefore, a rook move is possible if and only if the two numbers lie in the same row or the same column.
The spiral grid is generated by placing the numbers sequentially in a spiral pattern. For example, for \(n = 5\), the grid is:
[ \begin{matrix} 1 & 2 & 3 & 4 & 5 \ 16 & 17 & 18 & 19 & 6 \ 15 & 24 & 25 & 20 & 7 \ 14 & 23 & 22 & 21 & 8 \ 13 & 12 & 11 & 10 & 9 \end{matrix} ]
Your task is to implement the solution and process multiple queries.
inputFormat
The input is given via standard input and has the following format:
- The first line contains a single integer \(n\) (the size of the grid).
- The second line contains a single integer \(T\) (the number of queries).
- Each of the next \(T\) lines contains two space-separated integers representing the starting number and the target number.
It is guaranteed that all numbers are between 1 and \(n^2\) inclusive.
outputFormat
For each query, output a single line with either "YES" if a rook can move between the given positions (i.e. they lie in the same row or column), or "NO" otherwise. The output should be written to standard output.
## sample5
3
1 13
7 9
17 21
YES
YES
NO
</p>