Problem B
Beautiful Square

Harry loves drawing. Harry also loves squares. Today, Harry is going to draw a square.

Harry’s square can be divided into $n \cdot n$ unit squares. Let’s number the rows from $1$ to $n$ from top to bottom. Similarly, let’s number the columns from $1$ to $n$, from left to right.

In each unit square, Harry is going to write an integer between $1$ and $n \cdot n$. According to Harry, a square is beautiful iff it satisfies the following conditions:

  • Each integer from $1$ to $n \cdot n$ is written in exactly one cell.

  • For each $i$ from $1$ to $n \cdot n - 1$, the two integers $i$ and $i + 1$ must be written in two unit squares sharing an edge.

Harry has already drawn a square of size $n$ and has written the integer $1$ in some cell. Please help Harry write the integers from $2$ to $n \cdot n$ to create a beautiful square.


The first line of the input contains a single integer $t$ $(1 \le t \le 100)$ — the number of test cases. $t$ test cases follow, each test case contains three integers $n$, $r$, and $c$ in a single line $(1 \le r, c \le n \le 100)$, where:

  • $n$ represents the size of the square Harry has drawn.

  • $r$ and $c$ are the row and column of the unit square containing the integer $1$.


For each test case:

  • If it is not possible to create a beautiful square, print a single line containing ‘NO’.

  • Otherwise, print ‘YES’, followed by $n$ lines, each contains exactly $n$ integers, representing the beautiful square. If there are more than one solution, you can print any of them.

Sample Input 1 Sample Output 1
4 1 1
5 1 2
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13

Please log in to submit a solution to this problem

Log in