Skip to main content
•
Updated:
•
2 min read

N-Queens: Four Solvers Compared in Go

The N-Queens problem — place N queens on an N×N board so that none attack each other — is small enough to state in a sentence and rich enough to separate four very different search strategies. This project implements each one in Go and measures how they behave as N grows.

Written for a university assignment; published as a reference implementation, not a product.

The four solvers

Solver Strategy Guarantee
Exhaustive depth-first search Backtracking over every placement Complete; O(N!) worst case
Greedy hill climbing Best single move, with random restarts Can stall in local optima
Simulated annealing Probabilistic search with a cooling schedule Escapes local optima while hot
Genetic algorithm Population, crossover, mutation and elitism Strong exploration at large N

What the harness measures

main.go runs each solver across a range of board sizes and records wall-clock time and memory. Solved boards are printed to the terminal, so a result can be checked by eye as well as by the clock.

  • All four sit behind one interface — Solve, GetSolution, PrintSolution — so the benchmark loop does not care which one it is timing.
  • Backtracking is the correctness baseline; the heuristics are judged on the time they save as N grows.
  • Timings use Go’s time and runtime packages.

Run it

The source is browsable at public.omerduran.dev/omer/n-queens-algorithms-go and clonable from the same host:

git clone https://public.omerduran.dev/omer/n-queens-algorithms-go.git
cd n-queens-algorithms-go
go run .

License

MIT — see LICENSE in the repository.