We have discussed Knight’s tour and Rat in a Maze problems in Set 1 and Set 2 respectively. Let us discuss N Queen as another example 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 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 conflagrations
{
   generate the next configuration
   if queens don't attack in this configuration then
   {
      print this configuration;
   }
}
[ad type=”banner”]

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 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.

1) Start in the leftmost column
2) If all queens are placed
    return true
3) Try all rows in the current column.  Do following for every tried row.
    a) If the queen can be placed safely in this row then mark this [row, 
        column] as part of the solution and recursively check if placing  
        queen here leads to a solution.
    b) If placing queen in [row, column] leads to a solution then return 
        true.
    c) If placing queen doesn't lead to a solution then umark this [row, 
        column] (Backtrack) and go to step (a) to try other rows.
3) If all rows have been tried and nothing worked, return false to trigger 
    backtracking.

Implementation of Backtracking solution

JAVA Programming

[pastacode lang=”java” manual=”%2F*%20Java%20program%20to%20solve%20N%20Queen%20Problem%20using%0A%20%20%20backtracking%20*%2F%0Apublic%20class%20NQueenProblem%0A%7B%0A%20%20%20%20final%20int%20N%20%3D%204%3B%0A%20%0A%20%20%20%20%2F*%20A%20utility%20function%20to%20print%20solution%20*%2F%0A%20%20%20%20void%20printSolution(int%20board%5B%5D%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20N%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20(int%20j%20%3D%200%3B%20j%20%3C%20N%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(%22%20%22%20%2B%20board%5Bi%5D%5Bj%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2B%20%22%20%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println()%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20A%20utility%20function%20to%20check%20if%20a%20queen%20can%0A%20%20%20%20%20%20%20be%20placed%20on%20board%5Brow%5D%5Bcol%5D.%20Note%20that%20this%0A%20%20%20%20%20%20%20function%20is%20called%20when%20%22col%22%20queens%20are%20already%0A%20%20%20%20%20%20%20placeed%20in%20columns%20from%200%20to%20col%20-1.%20So%20we%20need%0A%20%20%20%20%20%20%20to%20check%20only%20left%20side%20for%20attacking%20queens%20*%2F%0A%20%20%20%20boolean%20isSafe(int%20board%5B%5D%5B%5D%2C%20int%20row%2C%20int%20col)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20i%2C%20j%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20Check%20this%20row%20on%20left%20side%20*%2F%0A%20%20%20%20%20%20%20%20for%20(i%20%3D%200%3B%20i%20%3C%20col%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(board%5Brow%5D%5Bi%5D%20%3D%3D%201)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20false%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20Check%20upper%20diagonal%20on%20left%20side%20*%2F%0A%20%20%20%20%20%20%20%20for%20(i%3Drow%2C%20j%3Dcol%3B%20i%3E%3D0%20%26%26%20j%3E%3D0%3B%20i–%2C%20j–)%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(board%5Bi%5D%5Bj%5D%20%3D%3D%201)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20false%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20Check%20lower%20diagonal%20on%20left%20side%20*%2F%0A%20%20%20%20%20%20%20%20for%20(i%3Drow%2C%20j%3Dcol%3B%20j%3E%3D0%20%26%26%20i%3CN%3B%20i%2B%2B%2C%20j–)%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(board%5Bi%5D%5Bj%5D%20%3D%3D%201)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20false%3B%0A%20%0A%20%20%20%20%20%20%20%20return%20true%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20A%20recursive%20utility%20function%20to%20solve%20N%0A%20%20%20%20%20%20%20Queen%20problem%20*%2F%0A%20%20%20%20boolean%20solveNQUtil(int%20board%5B%5D%5B%5D%2C%20int%20col)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F*%20base%20case%3A%20If%20all%20queens%20are%20placed%0A%20%20%20%20%20%20%20%20%20%20%20then%20return%20true%20*%2F%0A%20%20%20%20%20%20%20%20if%20(col%20%3E%3D%20N)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20true%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20Consider%20this%20column%20and%20try%20placing%0A%20%20%20%20%20%20%20%20%20%20%20this%20queen%20in%20all%20rows%20one%20by%20one%20*%2F%0A%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20N%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F*%20Check%20if%20queen%20can%20be%20placed%20on%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20board%5Bi%5D%5Bcol%5D%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(isSafe(board%2C%20i%2C%20col))%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F*%20Place%20this%20queen%20in%20board%5Bi%5D%5Bcol%5D%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20board%5Bi%5D%5Bcol%5D%20%3D%201%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F*%20recur%20to%20place%20rest%20of%20the%20queens%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(solveNQUtil(board%2C%20col%20%2B%201)%20%3D%3D%20true)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20true%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F*%20If%20placing%20queen%20in%20board%5Bi%5D%5Bcol%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20doesn’t%20lead%20to%20a%20solution%20then%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20remove%20queen%20from%20board%5Bi%5D%5Bcol%5D%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20board%5Bi%5D%5Bcol%5D%20%3D%200%3B%20%2F%2F%20BACKTRACK%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20If%20queen%20can%20not%20be%20place%20in%20any%20row%20in%0A%20%20%20%20%20%20%20%20%20%20%20this%20colum%20col%2C%20then%20return%20false%20*%2F%0A%20%20%20%20%20%20%20%20return%20false%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20This%20function%20solves%20the%20N%20Queen%20problem%20using%0A%20%20%20%20%20%20%20Backtracking.%20%20It%20mainly%20uses%20%20solveNQUtil()%20to%0A%20%20%20%20%20%20%20solve%20the%20problem.%20It%20returns%20false%20if%20queens%0A%20%20%20%20%20%20%20cannot%20be%20placed%2C%20otherwise%20return%20true%20and%0A%20%20%20%20%20%20%20prints%20placement%20of%20queens%20in%20the%20form%20of%201s.%0A%20%20%20%20%20%20%20Please%20note%20that%20there%20may%20be%20more%20than%20one%0A%20%20%20%20%20%20%20solutions%2C%20this%20function%20prints%20one%20of%20the%0A%20%20%20%20%20%20%20feasible%20solutions.*%2F%0A%20%20%20%20boolean%20solveNQ()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20board%5B%5D%5B%5D%20%3D%20%7B%7B0%2C%200%2C%200%2C%200%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B0%2C%200%2C%200%2C%200%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B0%2C%200%2C%200%2C%200%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B0%2C%200%2C%200%2C%200%7D%0A%20%20%20%20%20%20%20%20%7D%3B%0A%20%0A%20%20%20%20%20%20%20%20if%20(solveNQUtil(board%2C%200)%20%3D%3D%20false)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(%22Solution%20does%20not%20exist%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20false%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20printSolution(board)%3B%0A%20%20%20%20%20%20%20%20return%20true%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20driver%20program%20to%20test%20above%20function%0A%20%20%20%20public%20static%20void%20main(String%20args%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20NQueenProblem%20Queen%20%3D%20new%20NQueenProblem()%3B%0A%20%20%20%20%20%20%20%20Queen.solveNQ()%3B%0A%20%20%20%20%7D%0A%7D%0A%2F%2F%20This%20code%20is%20contributed%20by%20Abhishek%20Shankhadhar%0A” message=”java” highlight=”” provider=”manual”/]

Output: The 1 values indicate placements of queens

 0  0  1  0 
 1  0  0  0 
 0  0  0  1 
 0  1  0  0
[ad type=”banner”]