Camel
Time limit2sMemory limit512 MB
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.
- Level
Hard8 of 10
- Topics
- Greedy, Implementation, Math, Simulation
- Solved
- No attempts yet
Problem
Define 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 it can move to , , or , whenever the destination lies on the board. There are 8 possible moves in total.
The board is a square of cells, where 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 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.
Input
One line is read from standard input, containing a single integer .
Output
Write one of the following to standard output.
-
If the cycle is impossible, print
NOon one line. -
Otherwise, print lines. Each line contains space-separated integers. These integers are the distinct integers from to . The first number of the first line is . 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 is one move away from the start cell holding .
Constraints
- is a multiple of 5.
- .
Hint
Each number in the output is the visit order of its cell. The cells holding and are always one piece move apart, and so are the cells holding and . The starting cell is always the top-left corner with value .