The Knight's Tour - Problem
The Knight's Tour is a classic chess puzzle where you must move a knight across a chessboard so that it visits every square exactly once.
You're given a rectangular board with dimensions
Knight Movement Rules: A knight moves in an L-shape - exactly 2 squares in one direction and 1 square perpendicular to that, or vice versa. From position
• The destination is within bounds:
• The move forms an L-shape:
Return a 2D array where each cell contains the order of visit (starting from 0 for the initial position).
You're given a rectangular board with dimensions
m × n and a starting position (r, c) for the knight. Your task is to find a sequence of valid knight moves that visits all squares on the board exactly once.Knight Movement Rules: A knight moves in an L-shape - exactly 2 squares in one direction and 1 square perpendicular to that, or vice versa. From position
(r1, c1), a knight can move to (r2, c2) if:• The destination is within bounds:
0 ≤ r2 < m and 0 ≤ c2 < n• The move forms an L-shape:
min(|r1-r2|, |c1-c2|) = 1 and max(|r1-r2|, |c1-c2|) = 2Return a 2D array where each cell contains the order of visit (starting from 0 for the initial position).
Input & Output
example_1.py — Small 3×3 Board
$
Input:
m = 3, n = 3, r = 0, c = 0
›
Output:
[[0, 5, 2], [3, 8, 7], [6, 1, 4]]
💡 Note:
Starting from top-left corner (0,0), the knight visits all 9 squares. The numbers show the order of visits: start at 0, then move to position (2,1) for step 1, then to (0,2) for step 2, and so on.
example_2.py — Rectangular 4×3 Board
$
Input:
m = 4, n = 3, r = 1, c = 1
›
Output:
[[9, 4, 7], [2, 11, 6], [5, 8, 1], [10, 3, 0]]
💡 Note:
Starting from the middle position (1,1), the knight completes a tour of all 12 squares on this rectangular board. Note how the tour ends at position (3,2) with step 0 being the starting position.
example_3.py — Edge Case 1×1 Board
$
Input:
m = 1, n = 1, r = 0, c = 0
›
Output:
[[0]]
💡 Note:
On a 1×1 board, the knight can only occupy the single square, so the tour is trivially complete with just the starting position marked as step 0.
Constraints
- 1 ≤ m, n ≤ 8 (standard chessboard size limit)
- 0 ≤ r < m (starting row must be within bounds)
- 0 ≤ c < n (starting column must be within bounds)
- Note: Some board configurations may have no valid solution
Visualization
Tap to expand
Understanding the Visualization
1
Start Position
Place the knight at the given starting square and mark it as step 0
2
Analyze Options
From current position, identify all valid L-shaped moves the knight can make
3
Apply Warnsdorff's Rule
For each possible move, count future options and prioritize moves with fewer choices
4
Make Smart Move
Choose the move that leaves the most future flexibility (ironically, the most constrained next position)
5
Continue Recursively
Repeat the process from the new position until all squares are visited
Key Takeaway
🎯 Key Insight: Warnsdorff's heuristic works by choosing moves that preserve maximum future flexibility - a classic example of how being greedy about constraints can lead to optimal solutions.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code