{"record":{"id":"11e7f7b6d2853bd5","repo":"TheAlgorithms/C-Sharp","slug":"knight-tour-cannot-be-performed-on-a-board-of-size-n","errorCode":null,"errorMessage":"Knight Tour cannot be performed on a board of size {n}.","messagePattern":"Knight Tour cannot be performed on a board of size (.+?)\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"Algorithms/Problems/KnightTour/OpenKnightTour.cs","lineNumber":77,"sourceCode":"\n        var board = new int[n, n];\n\n        // Try every square as a starting point.\n        for (var r = 0; r < n; r++)\n        {\n            for (var c = 0; c < n; c++)\n            {\n                board[r, c] = 1; // first step\n                if (KnightTourHelper(board, (r, c), 1))\n                {\n                    return board;\n                }\n\n                board[r, c] = 0; // backtrack and try next start\n            }\n        }\n\n        throw new ArgumentException($\"Knight Tour cannot be performed on a board of size {n}.\");\n    }\n\n    /// <summary>\n    /// Recursively extends the current partial tour from <paramref name=\"pos\"/> after placing\n    /// move number <paramref name=\"current\"/> in that position.\n    /// </summary>\n    /// <param name=\"board\">The board with placed move numbers; <c>0</c> means unvisited.</param>\n    /// <param name=\"pos\">Current knight position (<c>Row</c>, <c>Col</c>).</param>\n    /// <param name=\"current\">The move number just placed at <paramref name=\"pos\"/>.</param>\n    /// <returns><c>true</c> if a full tour is completed; <c>false</c> otherwise.</returns>\n    /// <remarks>\n    /// Tries each legal next move in a fixed order (no heuristics). If a move leads to a dead end,\n    /// it backtracks by resetting the target cell to <c>0</c> and tries the next candidate.\n    /// </remarks>\n    private bool KnightTourHelper(int[,] board, (int Row, int Col) pos, int current)\n    {\n        if (IsComplete(board))\n        {","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Problems/KnightTour/OpenKnightTour.cs#L59-L95","documentation":"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.","triggerScenarios":"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.","commonSituations":"Small board sizes in tests or puzzles; users assuming a tour exists for every n; timeouts on large boards being misread as infeasibility.","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."],"exampleFix":"// before\nvar board = tour.Tour(3); // always throws\n// after\nif (n == 2 || n == 3) throw new InvalidOperationException($\"No open knight's tour exists for n={n}.\");\nvar board = tour.Tour(n);","handlingStrategy":"try-catch","validationCode":"bool feasible = n == 1 || n >= 5; // no open tour for n = 2, 3 (and 4 fails open tours on standard boards)","typeGuard":null,"tryCatchPattern":"try { board = tour.Tour(n); }\ncatch (ArgumentException) { Console.WriteLine($\"No open knight's tour exists for board size {n}.\"); }","preventionTips":["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"],"tags":["unsolvable-instance","knight-tour","backtracking"],"backgroundTag":"invalid-argument-value","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}