← All tools

N-Queens Solver

Place N queens on an N×N chessboard so that no two queens attack each other — the classic n-queens constraint-satisfaction problem. The solver uses backtracking: it places queens row by row, trying each column, and backtracks the moment a placement conflicts with a previously placed queen (same column or diagonal). Find the first solution, count and browse all solutions, or step through them. The number of solutions is a known sequence: N = 1, 2, …, 8 has 1, 0, 0, 2, 10, 4, 40, 92 distinct arrangements (the famous eight-queens puzzle has 92). Everything runs locally in your browser.

Board size N

The n-queens problem asks for placements of N non-attacking queens on an N×N board. The solver represents a board as an array cols of length N, where cols[r] is the column of the queen in row r — this guarantees no two queens share a row by construction, so the safety check only needs to test column and diagonal conflicts (|c₁−c₂| === |r₁−r₂| means same diagonal). Backtracking tries a column for the current row; if it conflicts, it tries the next; if none work, it backtracks to the previous row and re-places that queen. This explores the search tree depth-first, pruning branches as soon as a conflict appears. The solution count grows roughly exponentially: N=8 → 92, N=10 → 724, N=12 → 14 200 (computing all of those may take a moment). For N=2 and N=3 there are no solutions. Pairs with the Sudoku Solver, Knapsack Solver, and Subset-Sum Solver. Everything runs locally — nothing leaves your browser.