Problem. Place queens on an board so that no two attack each other (no shared row, column or diagonal). Count the solutions, or find one.

Backtracking with bitmasks — the standard solution

Place one queen per row; track occupied columns and both diagonal families as bitmasks.

long long count = 0;
void solve(int row, int cols, int d1, int d2, int n) {
    if (row == n) { count++; return; }
    int avail = ~(cols | d1 | d2) & ((1 << n) - 1);
    while (avail) {
        int bit = avail & -avail;
        avail ^= bit;
        solve(row + 1, cols | bit, (d1 | bit) << 1, (d2 | bit) >> 1, n);
    }
}
// call: solve(0, 0, 0, 0, n)

The shifts propagate the diagonals automatically: a queen at column in row blocks column of the "" family in row , which is exactly (d1 | bit) << 1.

avail & -avail isolates the lowest available column, so the loop visits each candidate once. runs in well under a second; takes a few seconds.

Solution counts

SolutionsSolutions
11892
209352
3010724
421214 200
510152 279 184
6420
74027 (the largest computed)

No closed form is known, and counting is believed hard. was computed in 2016 with a large distributed effort.

Constructing one solution in

Counting is hard; finding a single solution is easy. Explicit formulas exist for every :

  • even, : place queens at for and for the rest.
  • Other cases have similar formulas with small adjustments.

So “output any valid placement” is and needs no search at all — worth knowing, because contest versions usually ask for a solution, not the count.

Symmetry breaking

The board has 8 symmetries (the dihedral group ). Restricting the first queen to the first half of the first row roughly halves the search, and correcting for the fixed points recovers the exact count.

More aggressive: classify solutions by their symmetry class (asymmetric, 180°-symmetric, 90°-symmetric) and count each with the appropriate multiplier — a Burnside argument.

As exact cover

N-Queens is an exact cover problem with:

  • primary columns (must be covered exactly once): each rank, each file;
  • secondary columns (at most once): each of the diagonals.

Dancing Links with optional columns solves it directly, and this is the standard demonstration of secondary columns.

Variants

VariantNote
Count solutionsno closed form; by search
Find any solution construction
Solutions up to symmetry”fundamental” solutions; 12 for
Queens on a torussolvable iff
superqueens (queen + knight moves)much more constrained
Minimum queens to dominate the boarda different (domination) problem
Maximum non-attacking rookstrivially
Maximum non-attacking knights — one colour class
Maximum non-attacking bishops
Peaceable queens (two armies)an open problem

Why it is worth knowing

It is the canonical demonstration of three ideas:

  1. Bitmask state — three integers encode all the constraints, and the transitions are shifts.
  2. Backtracking with constraint checks — the pruning is free.
  3. Counting is harder than constructing — a recurring asymmetry (see also permanent vs determinant, and matchings).

See also: Backtracking · Dancing Links · Bit Operations