Link Search Close Search Menu Expand Document

N Queens Problem

N Queens is the problem of placing N chess queens on a NxN chess board, so that no two queens can attack each other
We can solve this problem using backtracking
We begin from leftmost column and try to place the queen on a row such that previously placed queens don’t attack the newly placed queen
If we find such block, we attempt to place the next queen on next column
If we don’t find such a block in current column, we backtrack and attempt to re-place the queen on previous column
If all the queens are placed on the board we have a solution
If we run out of all the blocks on the board, we don’t have a solution

Visualization of N Queens

Implementation of N Queens





Back to top

Copyright © 2020-2021 Gajanan Bhat. All rights reserved.