0% found this document useful (0 votes)
29 views

N-Queen Problem

The N-Queen problem involves placing N chess queens on an N×N chessboard so that no two queens threaten each other. The solution is represented as a matrix with 'Q's for queens and '.' for empty spaces, and the problem can be solved using a backtracking approach. The algorithm places queens column by column, checking for clashes and backtracking if necessary until a solution is found or all possibilities are exhausted.

Uploaded by

pushkar.pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

N-Queen Problem

The N-Queen problem involves placing N chess queens on an N×N chessboard so that no two queens threaten each other. The solution is represented as a matrix with 'Q's for queens and '.' for empty spaces, and the problem can be solved using a backtracking approach. The algorithm places queens column by column, checking for clashes and backtracking if necessary until a solution is found or all possibilities are exhausted.

Uploaded by

pushkar.pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

What is 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, the following is a solution for the 4 Queen problem.

The expected output is in the form of a matrix that has ‘Q‘s for the blocks
where queens are placed and the empty spaces are represented by ‘.’ . For
example, the following is the output matrix for the above 4-Queen solution.
.Q..
...Q
Q...
..Q.
N Queen Problem using Backtracking:
The idea is to place queens one by one in different columns, starting from the
leftmost column. When we place a queen in a column, we check for clashes with
already placed queens. In the current column, if we find a row for which there is
no clash, we mark this row and column as part of the solution. If we do not find
such a row due to clashes, then we backtrack and return false.
Below is the recursive tree of the above approach:

Recursive tree for N Queen problem

Follow the steps mentioned below to implement the idea:


 Start in the leftmost column
 If all queens are placed return true
 Try all rows in the current column. Do the following for every row.
o If the queen can be placed safely in this row
o Then mark this [row, column] as part of the solution and
recursively check if placing queen here leads to a
solution.
o If placing the queen in [row, column] leads to a solution
then return true.
o If placing queen doesn’t lead to a solution then unmark
this [row, column] then backtrack and try other rows.
o If all rows have been tried and valid solution is not found
return false to trigger backtracking.

You might also like