Construct a closed knight-like tour for a jumping camel piece on an N x N board where N is a multiple of 5, printing the visit order or NO.
Hard8GreedyImplementationMathSimulationNo attempts yetTime limit2sMemory limit512 MBDefine a new chess piece called camel-tone. It jumps two squares orthogonally to land three squares away, or one square diagonally to land two squares away. That is, from (r,c) it can move to (r±3,c), (r,c±3), or (r±2,c±2), whenever the destination lies on the board. There are 8 possible moves in total.
The board is a square of N×N cells, where N is always a multiple of 5.
The piece starts on the top-left corner cell (row 1, column 1). Play consists of a sequence of moves that visits every cell exactly once, and after N2−1 moves the piece is exactly one move away from its starting cell. Such a closed tour is called a camel-tone cycle.
Write a program camel that finds any such tour, or reports that the cycle is impossible.
One line is read from standard input, containing a single integer N.
Write one of the following to standard output.
If the cycle is impossible, print NO on one line.
Otherwise, print N lines. Each line contains N space-separated integers. These integers are the distinct integers from 1 to N2. The first number of the first line is 1. The output represents the board, and each integer gives the visit order of its cell. The cells holding two consecutive numbers are one piece move apart, and the cell holding N2 is one move away from the start cell holding 1.
Each number in the output is the visit order of its cell. The cells holding k and k+1 are always one piece move apart, and so are the cells holding N2 and 1. The starting cell is always the top-left corner with value 1.