N Queen Problem

The N Queen is the problem of placing N chess queens on an Nร—N chessboard so that no two queens attack each other. For example, following is a solution for 4 Queen problem.

The expected output is a binary matrix which has 1s for the blocks where queens are placed. For example, following is the output matrix for above 4 queen solution.

              { 0,  1,  0,  0}
              { 0,  0,  0,  1}
              { 1,  0,  0,  0}
              { 0,  0,  1,  0}

Naive Algorithm Generate all possible configurations of queens on board and print a configuration that satisfies the given constraints.

while there are untried configurations
{
   generate the next configuration
   if queens don't attack in this configuration then
   {
      print this configuration;
   }
}

Backtracking Algorithm The goal is to position queens in different columns one by one, starting with the leftmost column. We check for conflicts with already placed queens when placing a queen in a column. If we locate a row in the current column with no collision, we mark that row and column as part of the solution. We backtrack and return false if we can't discover such a row owing to conflicts.

Last updated

Was this helpful?