Summary: In this tutorial, we will learn what N Queen Problem is and how to solve N Queen Problem using the Backtracking algorithm in C and Java.
What is N Queen Problem?
We are given N chess’s queens and asked to place them in the chessboard of size NxN so that no two queens attack/eliminate each other.
A queen can only attack if another queen is either on:
- Same Row
- Same Column
- Same Diagonal (left or right)
Example: For the 4 Queens problem, we have a 4×4 board. If a queen is placed at (2,2), then the places where no queen should be placed are crossed by the red lines as shown in the figure:

Solution for N Queen Problem
Now the question is how to solve N Queen Problem?
There are two approaches to solve this problem:
Naïve Algorithm
In this approach, we use the brute force method by randomly choosing the position to find all possible solutions.
1 2 3 4 5 6 7 8 | <strong>while</strong> some configurations are unchecked { Generate configuration of queen position <strong>if</strong> <strong>in</strong> current configuration queens don't attack { print the configuration } } |
This approach is not efficient because it test all possible configuration which repeats same queens placements repeatedly.
Backtracking Algorithm
In this approach, we place a queen in one column, and then we proceed to the next column (since a single column cannot contain more than one queen).
We then check the possible safe row position to place the next queen, so that we can proceed to the next column.

If there is no safe row in the current column, we go back to the previous column and change the queen’s position to the next possible safe row position. If have any, we proceed to the next column again, else we go back further and repeat the same task.

If we reached the last column and found the possible safe position (i.e. row), we print the configuration of queens (i.e. solution), else if we came back to the first column and none of the position (i.e. row) is safe then we assume that there is no possible solution.
So, In backtracking, if no further solution is possible, we go one step back and proceed with next available option.
Check N-Queens Visualizer to get visual working of backtrack algorithm.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | find_solution(row, column) { if (column == lastColumn + 1) { passed all columns, therefore successfully found a solution; print board; return true; } else { for(all rows of column) { if([row, column] is safe for placing queen) { //place queen, i.e board[row][column] = 1; //check for next column if(find_solution(0,column + 1)) { return true; } //No safe place in column+1, remove queen, i.e board[row][column] = 0; } move to next row } checked all rows of the column, no safe place; return false; } } |
Here is the implementation of the algorithm in C and Java:
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | #include <stdio.h> #include <stdlib.h> #include <stdbool.h> void solveColumn(int); bool isValidPlace(int , int); void displayBoard(); void solveNqueen(); //Global variables int queens; int chessBoard[20][20]; bool hasSolution = false; int main() { //Enter value of N printf("Enter number of Queens \n"); scanf("%d", &queens); solveNqueen(); return 0; } void solveNqueen(){ //starting from (0,0) of the board solveColumn(0); if(!hasSolution) printf("No Solution \n"); } //Finding all solution by recusrive backtracking void solveColumn(int col){ //Reached beyond last column //Means solution (configuration) found if(col == queens){ hasSolution = true; displayBoard(); //intentionally returning to find more possible solution return; } for(int i=0; i<queens; i++){ //checking if position is safe if(isValidPlace(i,col)){ //setting current value to 1 means placing a queen chessBoard[i][col] = 1; //moving to next column's 1st row solveColumn(col+1); //backtrack - reset to 0 means removing queen chessBoard[i][col] = 0; } } } //To check whether the particular position is safe or not bool isValidPlace(int row, int col){ //Checking horizontally for(int i=col; i>=0; i--){ if(chessBoard[row][i] == 1) return false; } //checking left diagonal for(int i=row, j=col; i>=0 && j>=0; i--,j--){ if(chessBoard[i][j] == 1) return false; } //checking right diagonal for(int i=row, j=col; i<queens && j>=0; i++,j--){ if(chessBoard[i][j] == 1) return false; } return true; } //Display chess board with queen configuration void displayBoard(){ for(int i=0; i<queens; i++){ for(int j=0; j<queens; j++){ if(chessBoard[i][j] == 1) printf(" * "); else printf(" - "); } printf("\n"); } printf("\n\n"); } |
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | import java.util.*; class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); System.out.println("Enter Queens"); int n = in.nextInt(); NQueen nQueen = new NQueen(n); nQueen.solve(); } } class NQueen{ int n; int board[][]; boolean hasSolution = false; NQueen(int n){ this.n = n; board = new int[n][n]; } private void printBoard(){ for(int i=0; i<n; i++){ for(int j=0; j<n; j++){ if(board[i][j] == 1) System.out.print("* "); else System.out.print("- "); } System.out.println(); } System.out.println("\n \n"); } public void solve(){ solveColumn(0); if(!hasSolution) System.out.println("No Solution"); } private void solveColumn(int col){ if(col == n){ hasSolution= true; printBoard(); return; } for(int i=0; i<n; i++){ if(isValidPlace(i,col)){ board[i][col] = 1; solveColumn(col+1); } board[i][col] = 0; } } private boolean isValidPlace(int r, int c){ int i,j; for(i=r; i>=0; i--){ //up check if(board[i][c] == 1) return false; } for(j=c; j>=0; j--){ //left check if(board[r][j] == 1) return false; } //left up diagonal for(i=r,j=c; i>=0 && j>=0; i--,j--){ if(board[i][j] == 1) return false; } //left bottom diagonal for(i=r,j=c; i<n && j>=0; i++,j--){ if(board[i][j] == 1) return false; } return true; } } |
Output
When N is 4:

In the above program, the solveColumn(int col)
is the recursive function implementing the backtracking algorithm.
The function checks whether it is possible or not to place a queen in the specific column. If yes it proceeds to the next recursive call i.e. solveColumn(col+1)
else the execution returns back to the last recursive call to check for the next safe position to place the queens.
There are 92 safe configurations possible for 8 queens in the 8×8 chessboard, so it is not possible to show output here.
Check the output yourself by giving the value of N during program execution.
In this tutorial, we learned what is N Queen Problem and how to solve N queen problem using backtracking algorithm in C and Java programming languages.
wow,so helpful
thank you so much for saving me.
one request sir
how to solve sum of subsets problem using backtracking.
please help 👨🦱
Check out I have posted the solution of Subset problem.