The N queens puzzle is the problem of placing N chess queens on an N×N chessboard so that no two queens threaten each other. Thus, a solution requires that no two queens share the same row, column, or diagonal.

For example, below is one of the solution for famous 8 Queen problem.Branch and Bound | Set 5 (N Queen Problem)

Backtracking Algorithm for N-Queen is already discussed here. In backtracking solution we backtrack when we hit a dead end. In Branch and Bound solution, after building a partial solution, we figure out that there is no point going any deeper as we are going to hit a dead end.

Let’s begin by describing backtracking solution. “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.

Branch and Bound | Set 5 (N Queen Problem)

  1. For the 1st Queen, there are total 8 possibilities as we can place 1st Queen in any row of first column. Let’s place Queen 1 on row 3.
  2. After placing 1st Queen, there are 7 possibilities left for the 2nd Queen. But wait, we don’t really have 7 possibilities. We cannot place Queen 2 on rows 2, 3 or 4 as those cells are under attack from Queen 1. So, Queen 2 has only 8 – 3 = 5 valid positions left.
  3. After picking a position for Queen 2, Queen 3 has even fewer options as most of the cells in its column are under attack from the first 2 Queens.
[ad type=”banner”]

We need to figure out an efficient way of keeping track of which cells are under attack. In previous solution we kept an 8­-by­-8 Boolean matrix and update it each time we placed a queen, but that required linear time to update as we need to check for safe cells.

Basically, we have to ensure 4 things:
1. No two queens share a column.
2. No two queens share a row.
3. No two queens share a top-right to left-bottom diagonal.
4. No two queens share a top-left to bottom-right diagonal.

Number 1 is automatic because of the way we store the solution. For number 2, 3 and 4, we can perform updates in O(1) time. The idea is to keep three Boolean arrays that tell us which rows and which diagonals are occupied.

Lets do some pre-processing first. Let’s create two N x N matrix one for / diagonal and other one for \ diagonal. Let’s call them slashCode and backslashCode respectively. The trick is to fill them in such a way that two queens sharing a same /­diagonal will have the same value in matrix slashCode, and if they share same \­diagonal, they will have the same value in backslashCode matrix.

For an N x N matrix, fill slashCode and backslashCode matrix using below formula –

slashCode[row][col] = row + col
backslashCode[row][col] = row – col + (N-1)

Using above formula will result in below matrices

Branch and Bound | Set 5 (N Queen Problem)

The ‘N – 1’ in the backslash code is there to ensure that the codes are never negative because we will be using the codes as indices in an array.

[ad type=”banner”]

Now before we place queen i on row j, we first check whether row j is used (use an array to store row info). Then we check whether slash code ( j + i ) or backslash code ( j – i + 7 ) are used (keep two arrays that will tell us which diagonals are occupied). If yes, then we have to try a different location for queen i. If not, then we mark the row and the two diagonals as used and recurse on queen i + 1. After the recursive call returns and before we try another position for queen i, we need to reset the row, slash code and backslash code as unused again, like in the code from the previous notes.

Below is C++ implementation of above idea –

[pastacode lang=”cpp” manual=”%2F*%20C%2B%2B%20program%20to%20solve%20N%20Queen%20Problem%20using%20Branch%0A%20%20%20and%20Bound%20*%2F%0A%23include%3Cstdio.h%3E%0A%23include%3Cstring.h%3E%0A%23define%20N%208%0A%20%0A%2F*%20A%20utility%20function%20to%20print%20solution%20*%2F%0Avoid%20printSolution(int%20board%5BN%5D%5BN%5D)%0A%7B%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20N%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%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%20printf(%22%252d%20%22%2C%20board%5Bi%5D%5Bj%5D)%3B%0A%20%20%20%20%20%20%20%20printf(%22%5Cn%22)%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F*%20A%20Optimized%20function%20to%20check%20if%20a%20queen%20can%0Abe%20placed%20on%20board%5Brow%5D%5Bcol%5D%20*%2F%0Abool%20isSafe(int%20row%2C%20int%20col%2C%20int%20slashCode%5BN%5D%5BN%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20int%20backslashCode%5BN%5D%5BN%5D%2C%20bool%20rowLookup%5B%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20bool%20slashCodeLookup%5B%5D%2C%20bool%20backslashCodeLookup%5B%5D%20)%0A%7B%0A%20%20%20%20if%20(slashCodeLookup%5BslashCode%5Brow%5D%5Bcol%5D%5D%20%7C%7C%0A%20%20%20%20%20%20%20%20backslashCodeLookup%5BbackslashCode%5Brow%5D%5Bcol%5D%5D%20%7C%7C%0A%20%20%20%20%20%20%20%20rowLookup%5Brow%5D)%0A%20%20%20%20return%20false%3B%0A%20%0A%20%20%20%20return%20true%3B%0A%7D%0A%20%0A%2F*%20A%20recursive%20utility%20function%20to%20solve%20N%20Queen%20problem%20*%2F%0Abool%20solveNQueensUtil(int%20board%5BN%5D%5BN%5D%2C%20int%20col%2C%0A%20%20%20%20int%20slashCode%5BN%5D%5BN%5D%2C%20int%20backslashCode%5BN%5D%5BN%5D%2C%20bool%20rowLookup%5BN%5D%2C%0A%20%20%20%20bool%20slashCodeLookup%5B%5D%2C%20bool%20backslashCodeLookup%5B%5D%20)%0A%7B%0A%20%20%20%20%2F*%20base%20case%3A%20If%20all%20queens%20are%20placed%0A%20%20%20%20then%20return%20true%20*%2F%0A%20%20%20%20if%20(col%20%3E%3D%20N)%0A%20%20%20%20%20%20%20%20return%20true%3B%0A%20%0A%20%20%20%20%2F*%20Consider%20this%20column%20and%20try%20placing%0A%20%20%20%20%20%20%20this%20queen%20in%20all%20rows%20one%20by%20one%20*%2F%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20N%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%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%20board%5Bi%5D%5Bcol%5D%20*%2F%0A%20%20%20%20%20%20%20%20if%20(%20isSafe(i%2C%20col%2C%20slashCode%2C%20backslashCode%2C%20rowLookup%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20slashCodeLookup%2C%20backslashCodeLookup)%20)%0A%20%20%20%20%20%20%20%20%7B%0A%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%20board%5Bi%5D%5Bcol%5D%20%3D%201%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20rowLookup%5Bi%5D%20%3D%20true%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20slashCodeLookup%5BslashCode%5Bi%5D%5Bcol%5D%5D%20%3D%20true%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20backslashCodeLookup%5BbackslashCode%5Bi%5D%5Bcol%5D%5D%20%3D%20true%3B%0A%20%0A%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%20if%20(%20solveNQueensUtil(board%2C%20col%20%2B%201%2C%20slashCode%2C%20backslashCode%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20rowLookup%2C%20slashCodeLookup%2C%20backslashCodeLookup)%20)%0A%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%2F*%20If%20placing%20queen%20in%20board%5Bi%5D%5Bcol%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20doesn’t%20lead%20to%20a%20solution%2C%20then%20backtrack%20*%2F%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F*%20Remove%20queen%20from%20board%5Bi%5D%5Bcol%5D%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20board%5Bi%5D%5Bcol%5D%20%3D%200%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20rowLookup%5Bi%5D%20%3D%20false%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20slashCodeLookup%5BslashCode%5Bi%5D%5Bcol%5D%5D%20%3D%20false%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20backslashCodeLookup%5BbackslashCode%5Bi%5D%5Bcol%5D%5D%20%3D%20false%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*%20If%20queen%20can%20not%20be%20place%20in%20any%20row%20in%0A%20%20%20%20%20%20%20%20this%20colum%20col%20then%20return%20false%20*%2F%0A%20%20%20%20return%20false%3B%0A%7D%0A%20%0A%2F*%20This%20function%20solves%20the%20N%20Queen%20problem%20using%0ABranch%20and%20Bound.%20It%20mainly%20uses%20solveNQueensUtil()%20to%0Asolve%20the%20problem.%20It%20returns%20false%20if%20queens%0Acannot%20be%20placed%2C%20otherwise%20return%20true%20and%0Aprints%20placement%20of%20queens%20in%20the%20form%20of%201s.%0APlease%20note%20that%20there%20may%20be%20more%20than%20one%0Asolutions%2C%20this%20function%20prints%20one%20of%20the%0Afeasible%20solutions.*%2F%0Abool%20solveNQueens()%0A%7B%0A%20%20%20%20int%20board%5BN%5D%5BN%5D%3B%0A%20%20%20%20memset(board%2C%200%2C%20sizeof%20board)%3B%0A%20%0A%20%20%20%20%2F%2F%20helper%20matrices%0A%20%20%20%20int%20slashCode%5BN%5D%5BN%5D%3B%0A%20%20%20%20int%20backslashCode%5BN%5D%5BN%5D%3B%0A%20%0A%20%20%20%20%2F%2F%20arrays%20to%20tell%20us%20which%20rows%20are%20occupied%0A%20%20%20%20bool%20rowLookup%5BN%5D%20%3D%20%7Bfalse%7D%3B%0A%20%0A%20%20%20%20%2F%2Fkeep%20two%20arrays%20to%20tell%20us%20which%20diagonals%20are%20occupied%0A%20%20%20%20bool%20slashCodeLookup%5B2*N%20-%201%5D%20%3D%20%7Bfalse%7D%3B%0A%20%20%20%20bool%20backslashCodeLookup%5B2*N%20-%201%5D%20%3D%20%7Bfalse%7D%3B%0A%20%0A%20%20%20%20%2F%2F%20initalize%20helper%20matrices%0A%20%20%20%20for%20(int%20r%20%3D%200%3B%20r%20%3C%20N%3B%20r%2B%2B)%0A%20%20%20%20%20%20%20%20for%20(int%20c%20%3D%200%3B%20c%20%3C%20N%3B%20c%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20slashCode%5Br%5D1%20%3D%20r%20%2B%20c%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20backslashCode%5Br%5D1%20%3D%20r%20-%20c%20%2B%207%3B%0A%20%0A%20%20%20%20if%20(solveNQueensUtil(board%2C%200%2C%20slashCode%2C%20backslashCode%2C%0A%20%20%20%20%20%20rowLookup%2C%20slashCodeLookup%2C%20backslashCodeLookup)%20%3D%3D%20false%20)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20printf(%22Solution%20does%20not%20exist%22)%3B%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%2F%20solution%20found%0A%20%20%20%20printSolution(board)%3B%0A%20%20%20%20return%20true%3B%0A%7D%0A%20%0A%2F%2F%20driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20solveNQueens()%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D%0A” message=”” highlight=”” provider=”manual”/]

 

Output :

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