Uncrossed Knight's Tour
Time limit2sMemory limit1024 MB
Given an m by n board (m at most 8, n up to 1e15), find the maximum number of squares a closed knight tour can visit without crossing itself.
- Level
Hard9 of 10
- Topics
- Greedy, Dynamic programming, Math, Implementation
- Solved
- No attempts yet
Problem
A well-known puzzle is to tour all the squares of an chessboard with a knight, a piece that moves only by jumping one square in one direction and two squares in an orthogonal direction. The knight must visit every square of the board, without repeats, and then return to its starting square. There are many ways to do this, and the board is small enough that a person can solve the puzzle by hand.
This is a harder version, played on a rectangular board with one extra constraint: the knight may never cross its own path. Picture the path as straight line segments joining the centers of the squares the knight jumps between. Those segments must form a simple polygon, so no two segments intersect or touch, except that consecutive segments touch at their common end point. Under this constraint the knight cannot reach every square, so you must instead maximize the number of squares it visits. The knight must still return to its starting square.
Figure 1 shows an optimal tour on a board.

Figure 1: An optimal tour on a board.
Input
The input consists of a single line containing two integers () and (), the dimensions of the rectangular board.
Output
Display the largest number of squares that a knight can visit in a tour on an board that does not cross its own path. If no such tour exists, display .