N-Queen Problem
N-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: