N Queen Problem
- Let us discuss N Queen problem that can be solved using Backtracking.
- 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 the solution for 4 Queen problem.

- Binary matrix which has 1’s for the blocks where queens are placed. For example, following is the output matrix for above 4 queen solution.
Backtracking Algorithm
- 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 can checkout to 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.