Here you will get program for N queens problem in C using backtracking.

N Queens Problem is a famous puzzle in which n-queens are to be placed on a nxn chess board such that no two queens are in the same row, column or diagonal. In this tutorial I am sharing the C program to find solution for N Queens problem using backtracking. Below animation shows the solution for 8 queens problem using backtracking.

**Also Read: C Program for Tower of Hanoi Problem**

8 Queens Problem Using Backtracking |

## Program for N Queens Problem in C Using Backtracking

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 |
#include<stdio.h> #include<math.h> int board[20],count; int main() { int n,i,j; void queen(int row,int n); printf(" - N Queens Problem Using Backtracking -"); printf("\n\nEnter number of Queens:"); scanf("%d",&n); queen(1,n); return 0; } //function for printing the solution void print(int n) { int i,j; printf("\n\nSolution %d:\n\n",++count); for(i=1;i<=n;++i) printf("\t%d",i); for(i=1;i<=n;++i) { printf("\n\n%d",i); for(j=1;j<=n;++j) //for nxn board { if(board[i]==j) printf("\tQ"); //queen at i,j position else printf("\t-"); //empty slot } } } /*funtion to check conflicts If no conflict for desired postion returns 1 otherwise returns 0*/ int place(int row,int column) { int i; for(i=1;i<=row-1;++i) { //checking column and digonal conflicts if(board[i]==column) return 0; else if(abs(board[i]-column)==abs(i-row)) return 0; } return 1; //no conflicts } //function to check for proper positioning of queen void queen(int row,int n) { int column; for(column=1;column<=n;++column) { if(place(row,column)) { board[row]=column; //no conflicts so place queen if(row==n) //dead end print(n); //printing the board configuration else //try queen with next position queen(row+1,n); } } } |

**Output**

Comment below if you found anything incorrect in above N queens problem in C.

#include will not declared in the scope fix it then it will work 🙂 thanks again to build up this dashing structure .

Hey Neeraj! can you please explain the code. I have got manual logic of n queen problem but finding code difficult to understand like board[i]=column and other such lines. Does board[i] or board[row] has some value. Please explain, i am a beginner….

I tried but i am not able to understand the logic :-

if (abs(board[i]-column)==abs(i-row))

How, it is able to check the column. please explain.

Actually it is used to check that queen should not be placed diagonally.

thanks for the code, but I have a doubt.

consider the first two queens are placed at (row 1, column1) and (row2 ,column 3). now the next queen can not be placed in row 3 and hence the position of 2nd queen is changing from (row2, column3) to (row2 ,column4).

since, I can not see any code line to decrement the row number. can you please tell me how it is really executing this step???

same question…….how is the row decrementing?

thank you so much bro 🙂 its hard to understand algorithm from textbook….. but u made it simple to understand through this program… thank you once again 🙂

This is DFS algortihm or BFS algorithm?

explain in detail how place function is working. and what is abs?

abs means absolute