#P1549. [NOIP 1997 提高组] 棋盘问题

[NOIP 1997 提高组] 棋盘问题

Description

On an N×NN \times N (1N101 \le N \le 10) board, fill in the N2N ^ 2 numbers 1,2,,N21, 2, \dots, N ^ 2, so that the sum of any two adjacent numbers is a prime number.

For example, when N=2N = 2, there is:

11 22
44 33

The adjacent pairs whose sums are prime are:

1+2,1+4,4+3,2+31+2,1+4,4+3,2+3

When N=4N=4, one possible arrangement is:

11 22 1111 1212
1616 1515 88 55
1313 44 99 1414
66 77 1010 33

Here we stipulate that the top-left cell must contain the number 11.

Input Format

A single integer NN.

Output Format

If multiple solutions exist, output the arrangement whose sum of the first row and the first column is minimal. Output the board in NN lines, each containing NN integers separated by single spaces. If no solution exists, output NO.

1
NO

2
1 2
4 3

Hint

Translated by ChatGPT 5