PlayNook

Math & Logic · Graph Theory

Knight's Tour

A knight, every square once, and never the same square twice. The rule that solves it is on the board while you play — and there is a two-player game hiding in the same rules.

A knight moves in an L. Ask it to visit all sixty-four squares of a chessboard exactly once and you have a puzzle that Euler wrote a paper about in 1759 and that first-year computing courses still set every autumn.

The hard part is not running out of moves. It is stranding a square — leaving some far corner with no unvisited way in, forty jumps before you find out.

How to play the Knight's Tour

A chessboard twenty moves into a tour, with the visited squares numbered and each reachable square showing how many onward moves it has, the smallest highlighted
A real position from the solver on this page. Four squares are reachable; the number on each is how many ways out it has. Warnsdorff says take the smallest.

In 1823 H. C. von Warnsdorff wrote down a rule that fits in one line: always jump to the square with the fewest onward moves.

It sounds like exactly the wrong advice. You are deliberately heading for the cramped squares, the corners, the edges — the places with no room. But that is the whole idea: a square with few ways out is the one you will not be able to reach later, so you go there while you still can. The roomy squares in the middle will still be roomy in thirty moves.

Those numbers are printed on the board while you play. Follow them and you have played the rule without learning it — and, more usefully, you will feel the moment it stops helping.

Warnsdorff's rule: how well does it work?

Most pages tell you Warnsdorff's rule "usually works". We ran it.

An eight by eight board where each square shows the percentage of tours completed when starting there, mostly between 95 and 99
Four hundred tours from each of the sixty-four starting squares, ties broken at random. The average is 97.9%, and not one square is completely safe.

97.9% on average. The worst starting square manages about 95%. Not a single square reaches a clean 100%.

The failures live entirely in the ties. Warnsdorff's rule tells you what to do when one square has the fewest ways out — it says nothing at all about what to do when two of them tie, and on a chessboard they tie constantly. Break the ties differently and you get a different rule with a different success rate, which is why "Warnsdorff's rule" is really a family of rules.

A rule that is right 98% of the time and costs one glance is worth more than a search that is always right and costs a million steps. That is the entire case for heuristics, in one puzzle.

Which boards have a knight's tour?

Not every board does, and the exceptions are not where you would look for them.

A grid of board sizes up to eight by eight, green where a tour exists and red where none does, with 3x3, 3x5, 3x6 and 4x4 marked in red
Every board up to 8×8, searched exhaustively from every starting square — not looked up. Four boards fail for reasons you cannot see by staring at them.

Setting aside 1×n and 2×n, where a knight can barely move at all, exactly four boards have no tour from any square: 3×3, 3×5, 3×6 and 4×4.

The 3×3 is easy to see — the centre square has no knight moves at all, so it can never be visited. The other three are not obvious in the slightest, and 3×6 is the one that catches people out, because 3×4 works and 3×7 works. There is no pattern you can feel; you have to check.

A closed tour, where the last square is a knight's move from the first, is rarer still. Schwenk proved exactly which boards have one; we checked his statement against our own search on eleven boards rather than repeating it, and it held every time.

The two-player game hiding in the same rules

You cannot share a tour. You can fight over one.

In the duel, both players move the same knight in turn, never onto a square it has already left, and whoever cannot move loses. The rules are the ones above with one word changed, and it is a proper combinatorial game — not something we invented to have a multiplayer mode.

Three boards, 5x5, 7x7 and 8x8, with each square marked green if the player to move wins from it; the odd boards show a checkerboard pattern, the 8x8 is entirely green
Computed for every square. On odd boards the winning squares are exactly the less common colour; on even boards, every square wins.

Who wins depends entirely on where the knight starts, which is why this page deals the starting square at random instead of letting the first player choose it.

On a board with an even number of squares — 6×6, 8×8 — the player to move wins from every square. On a board with an odd number, they win from exactly the squares of the less common colour: 12 of 25 on a 5×5, 24 of 49 on a 7×7. So on 8×8 the duel is a first-player win and the question is only whether you can find the moves; on 5×5 and 7×7 the coin toss at the start decides who should win, and the game is about not throwing it away.

Why matchings decide it

The rule behind that picture is short enough to state and strange enough to be worth stating.

Pair up squares so that each pair is a single knight's move — a matching. A maximum matching is one you cannot make any bigger. Then:

The player to move wins exactly when every maximum matching of the remaining board covers the square the knight is standing on.

The winning move is simply to jump to your partner in such a matching. If some maximum matching manages to leave your square out, you have lost with correct play, and there is nothing to be done.

This is the same shape of answer as the nim-sum in Nim: a complete theory, computable in milliseconds, on a game whose search tree would never finish. A knight alternates square colour with every jump, which makes the board a bipartite graph, which makes the matching easy to compute.

We did not take it on trust. Before building anything on it we compared it against exhaustive game-tree search over 282,068 positions on five boards. Every one agreed.

Where does the Knight's Tour come from?

The tour is old. Euler presented a paper on it to the Berlin Academy in 1759, and versions of the problem appear in Sanskrit poetry centuries before that — the Kavyalankara contains a knight's tour written as verse, where the syllables read in tour order give the poem twice over.

The count of tours on a standard board is enormous and was not settled until computers were pointed at it: there are 26,534,728,821,064 directed closed tours on 8×8. That number is not ours — it took a dedicated computation, and we have not repeated it. Everything else on this page we worked out ourselves.

If you like a puzzle whose answer is known before you start, the Towers of Hanoi does the same thing with arithmetic; if you would rather have an opponent and a complete theory, that is Nim.

Frequently asked questions

+ What is the Knight's Tour?

A knight has to visit every square of a chessboard exactly once, moving only as a knight moves. On a standard 8×8 board there are trillions of ways to do it, and finding one by hand is much harder than that makes it sound — the difficulty is not running out of moves early, it is stranding a square you can no longer reach.

+ What is Warnsdorff's rule?

A rule from 1823: always jump to the square that has the fewest onward moves. It sounds backwards — you deliberately head for the cramped squares — but that is the point. A square with few ways out is the one you will not be able to reach later, so you visit it while you still can. This page prints that number on every square you can reach, so you can play the rule without having to learn it.

+ Does Warnsdorff's rule always work?

No, and we measured it rather than guessing. Running 400 tours from each of the 64 starting squares, breaking ties at random, the rule completes a tour 97.9% of the time on average. Not one starting square is completely safe, and the worst is around 95%. That is still remarkable for a rule you can apply in your head in a second — a backtracking search is always right and takes millions of steps.

+ Which boards have no knight's tour?

We searched every board up to 8×8 exhaustively, from every starting square. Beyond the obvious 1×n and 2×n boards, exactly four fail: 3×3, 3×5, 3×6 and 4×4. The 3×6 is the one that catches people, because 3×4 and 3×7 both work.

+ What is a closed tour?

One where the last square is a knight's move from the first, so the tour is a loop you can start anywhere. They are rarer. A board with m ≤ n has no closed tour if m and n are both odd, or m is 1, 2 or 4, or m is 3 and n is 4, 6 or 8 — a result of Schwenk's, which we checked against our own search on eleven boards rather than repeating it.

+ How do two people play a knight's tour?

They do not share a tour — they fight over one. In the duel both players move the same knight in turn, never onto a square it has already left, and whoever cannot move loses. It is a proper game with a complete theory, and you can play it online here against anyone you send a table code to.

+ Who wins the duel?

It depends entirely on where the knight starts. On a board with an even number of squares — 6×6, 8×8 — the player to move wins from every square. On a board with an odd number, they win from exactly the squares of the less common colour: 12 of 25 on a 5×5, 24 of 49 on a 7×7. That is why the starting square here is dealt at random rather than chosen.

+ What decides it mathematically?

Maximum matchings. The player to move wins exactly when every maximum matching of the remaining board covers the square the knight is standing on. It is the same kind of complete answer as the nim-sum in Nim, with a different tool — and it runs in milliseconds where a game-tree search on 8×8 would never finish. We checked the claim against exhaustive search over 282,068 positions before trusting it.

+ Is it free?

Yes, and there is no sign-up, for the online duels either. Your best result on each board is kept in your own browser.

More games like this