Problem

4 /10


horse filling

Problem

Given an nxn chessboard. Let the knight stand on the cell (1,1). It is necessary to find such a sequence of moves of the knight, in which he visits each square of the board exactly once.
 
Input
The input to the program is a natural number n (n ≤ 8).
 
Output
If the bypass is impossible, then output 0 to the output file, if possible, then 1, and on the next lines print the matrix nn, illustrating the order of the bypass. It is not necessary to align numbers by columns.
 
Note. The speed of the recursive program in this problem essentially depends on the order in which the variants of the knight's move from the next cell will be considered. One good order is to place all eight options "in a circle".
 
Input Output
3 0
5
1
1 20 17 12 3 
16 11 2 7 18 
21 24 19 4 13 
10 15 6 23 8 
25 22 9 14 5