DevTools Logo

Backtracking Solver Lab

Backtracking Solver Lab

Explore deterministic Sudoku and N-Queens constraint solving, with a search limit so the tab never freezes.

Runs in your browser
Try an example

Sudoku givens

30 givens

Type 1-9 in a cell, or paste an 81-character puzzle (. or 0 for empty).

Sudoku solution

5
3
4
6
7
8
9
1
2
6
7
2
1
9
5
3
4
8
1
9
8
3
4
2
5
6
7
8
5
9
7
6
1
4
2
3
4
2
6
8
5
3
7
9
1
7
1
3
9
2
4
8
5
6
9
6
1
5
3
7
2
8
4
2
8
7
4
1
9
6
3
5
3
4
5
2
8
6
1
7
9

Solution found, the only one

51assignments
0backtracks
52search nodes

About

Enter a Sudoku puzzle or choose an N-Queens board size. The local solver uses minimum remaining values for Sudoku, displays the first N-Queens solution, counts all solutions through N=8, and reports assignments, backtracks, and search nodes.

Examples

Contradictory row rejected

Input
solveSudoku([5, 5, 0, ...79 more])
Output
{ solved: false, reason: "invalid" }

Two 5s in one row can never satisfy Sudoku rules, so validation fails before search.

4-queens has 2 solutions

Input
solveNQueens(4)
Output
{ solved: true, solutionCount: 2, firstSolution: [...] }

The 4x4 board admits exactly two placements up to the tool's full counting (size <= 8).

About this tool

Backtracking Solver Lab searches constraint problems with solveSudoku(grid) and solveNQueens(size): Sudoku takes an 81-number grid (0 = empty) under MRV search (minimum-remaining-values cell choice), returning solved, grid, steps (assign/backtrack with candidates), assignments, backtracks, nodes, and reason invalid/no-solution. N-Queens takes size 1–14 via row-by-row placement with queenSafe diagonal checks.

Sudoku validation is strict: wrong length, non-integers, out-of-range values, or any given that already violates row/column/box rules returns reason invalid with zero search. N-Queens counts every solution only for size <= 8 (N=8 yields 92 with the first solution exposed); larger sizes return the first solution with solutionCount null.

Search is fully deterministic: the same puzzle always yields the same grid and step log, given cells are never overwritten, and isValidSudokuSolution independently verifies any completed grid.

How to use this tool

  1. Enter the puzzle or board size

    Provide 81 Sudoku values (0 for blanks) or an N-Queens size between 1 and 14.

  2. Run the solver

    Solve once — read solved plus reason (invalid means the givens already clash).

  3. Replay assign/backtrack steps

    Walk steps to see MRV cell picks, candidate lists, assignments, and where backtracks unwind.

  4. Verify the result

    Confirm givens are untouched and run isValidSudokuSolution (or check firstSolution for queens).

Use cases

Visualizing MRV search

Show how picking the most-constrained cell first cuts assignments versus naive order.

Checking puzzle validity

Distinguish an invalid given set from a valid-but-unsolvable grind via the reason field.

Enumerating small queen boards

Count all solutions for N <= 8 (N=4 gives 2, N=8 gives 92) and inspect the first layout.

Common mistakes

Mistake:Reading reason invalid as search exhausted the space.

Fix:Invalid means the input givens already break Sudoku rules — no search ran. Only no-solution means a valid grid with no completion.

Mistake:Expecting solutionCount for large N.

Fix:Sizes above 8 return solutionCount null by design (combinatorial explosion) — only solved/firstSolution plus placements/backtracks/nodes are reported.

Frequently asked questions

What does this tool do?

It solves constraints with solveSudoku (81-cell grid, MRV search, assign/backtrack steps with candidates, assignments/backtracks/nodes stats) and solveNQueens (size 1–14, full counting for size <= 8 — N=4 gives 2, N=8 gives 92), plus isValidSudokuSolution verification.

What is the difference between invalid and no-solution?

invalid means the givens already violate Sudoku rules (e.g. two 5s in a row) so no search ran; no-solution means a rule-clean grid that still has no completion. Both return solved false.

Is my puzzle uploaded or stored?

No. Validation, MRV search, and queen placement run fully client-side and deterministically.

Why is solutionCount null for large N?

Full counting is capped at size <= 8 to avoid combinatorial explosion — larger boards still return solved and firstSolution with placements/backtracks/nodes, but solutionCount is null.

References & standards