N-Queens Visualizer

Place queens row by row, reject attacked squares, and watch backtracking discover valid N-Queens boards.

Board
4x4
Queens
0
Solutions
0
Step
1/95

Board setup

Step controls

Step 1 of 95: Start solving 4-Queens by placing one queen per row.

N-Queens board

Start solving 4-Queens by placing one queen per row.

Queen Attacked Rejected

Call stack

No queens are currently placed.

Solutions

No solutions recorded in this step yet.

What is an N-Queens Visualizer?

An N-Queens visualizer shows how backtracking places queens on an N by N chessboard so no two queens share a row, column, or diagonal.

This tool steps through each placement attempt, rejection, queen placement, solution, and backtrack. It highlights attacked squares so the pruning logic is visible.

For the full cluster of related tools, browse the Data Structure Visualizers hub.

How to use this N-Queens visualizer

  • Choose a board size from 4 to 8.
  • Step through row-by-row queen placement.
  • Watch rejected squares when a column or diagonal is attacked.
  • Run to the end to record solutions.
  • Compare the board with the call stack and solution list.

N-Queens backtracking idea

The solver places one queen per row. For each row, it tries every column. If a column or diagonal is already occupied, that square is rejected. If a square is safe, the queen is placed and the solver moves to the next row.

When a row has no safe column, the solver removes the previous queen and tries the next column. This is the backtracking step.

Compare this page with the Backtracking Visualizer, Recursion Tree Visualizer, and Matrix Traversal Visualizer.

Frequently Asked Questions

What does N mean in N-Queens?
N is both the number of queens and the board size. A 4-Queens board is 4 by 4 and needs four queens.
Why is N-Queens solved with backtracking?
Each queen placement creates a partial board. If the partial board cannot lead to a valid solution, the algorithm undoes the choice and tries another column.
What makes a square unsafe?
A square is unsafe if an existing queen shares the same column, major diagonal, or minor diagonal.
Does this visualizer show all N-Queens solutions?
It records up to four solutions per run so the visualizer remains readable for larger board sizes.

Related tools