TheAlgorithms/C-Sharp · error
Knight Tour cannot be performed on a board of size
Error message
Knight Tour cannot be performed on a board of size {n}. What it means
After trying every square as a starting point, Tour throws ArgumentException if no open knight's tour was found for the given board size n. Some board sizes (notably 2, 3, and small edge cases) admit no open knight's tour at all, so failure is a mathematical property of the input, not a bug.
Solutions
- Use a board size known to admit an open tour (n >= 5, or valid small boards like 1).
- Catch ArgumentException and inform the user that no tour exists for that size.
- Check board-size feasibility (knight's tour theory) before invoking.
Example fix
// before
var board = tour.Tour(3); // always throws
// after
if (n == 2 || n == 3) throw new InvalidOperationException($"No open knight's tour exists for n={n}.");
var board = tour.Tour(n); Defensive patterns
Strategy: try-catch
Validate before calling
bool feasible = n == 1 || n >= 5; // no open tour for n = 2, 3 (and 4 fails open tours on standard boards)
Try / catch
try { board = tour.Tour(n); }
catch (ArgumentException) { Console.WriteLine($"No open knight's tour exists for board size {n}."); } Prevention
- Restrict inputs to sizes known to admit tours (n >= 5)
- Inform users of infeasible sizes up front
- Treat the exception as 'no solution exists', not a bug
When it happens
Trigger: Calling Tour(2) or Tour(3) — boards on which no open knight's tour exists; any n where the backtracking search exhausts all starting squares without success.
Common situations: Small board sizes in tests or puzzles; users assuming a tour exists for every n; timeouts on large boards being misread as infeasibility.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/11e7f7b6d2853bd5.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Problems/KnightTour/OpenKnightTour.cs:77
var board = new int[n, n];
// Try every square as a starting point.
for (var r = 0; r < n; r++)
{
for (var c = 0; c < n; c++)
{
board[r, c] = 1; // first step
if (KnightTourHelper(board, (r, c), 1))
{
return board;
}
board[r, c] = 0; // backtrack and try next start
}
}
throw new ArgumentException($"Knight Tour cannot be performed on a board of size {n}.");
}
/// <summary>
/// Recursively extends the current partial tour from <paramref name="pos"/> after placing
/// move number <paramref name="current"/> in that position.
/// </summary>
/// <param name="board">The board with placed move numbers; <c>0</c> means unvisited.</param>
/// <param name="pos">Current knight position (<c>Row</c>, <c>Col</c>).</param>
/// <param name="current">The move number just placed at <paramref name="pos"/>.</param>
/// <returns><c>true</c> if a full tour is completed; <c>false</c> otherwise.</returns>
/// <remarks>
/// Tries each legal next move in a fixed order (no heuristics). If a move leads to a dead end,
/// it backtracks by resetting the target cell to <c>0</c> and tries the next candidate.
/// </remarks>
private bool KnightTourHelper(int[,] board, (int Row, int Col) pos, int current)
{
if (IsComplete(board))
{View on GitHub (pinned to 96e2905cab)