#include
using namespace std;
bool is_it_safe(int board[][10],int i,int j,int n){
//For Coloumn
for(int row=0;row<i;row++){
if(board[row][j]==1);
return false;
}
//For left side
int x = i-1 ;
int y = j-1 ;
while(x>=0 && y>=0){
if(board[x][y] == 1){
return false;
}
x–;
y–;
}
//For right side
x = i-1 ;
y = j+1 ;
while(x>=0 && y<n){
if(board[x][y] == 1){
return false;
}
x–;
y++;
}
return true;
}
bool solve_N_queen(int board[][10],int i,int n){
//Base Case->when you have successfully placed the queen at their right position.
if(i==n){
//Print the board.
for(int row=0;row<n;row++){
for(int col = 0;col<n;col++){
if(board[row][col] == 1){
cout<<"Q “;
}
else{
cout<<”_ ";
}
}
cout<<endl;
}
return true;
}
//Recursive Case
//Try to place the queen in the current row and recursion will solve the problem for remaining rows
for(int j = 0;j<n;j++){
if(is_it_safe(board,i,j,n)){
board[i][j] = 1;
bool next = solve_N_queen(board,i+1,n);
if(next == true){
return true;
}
board[i][j] = 0;
}
}
return false;
}
int main(){
cout<<“Enter The N :”;int n;
cin>>n;
int board[10][10] = {0};
solve_N_queen(board , 0 , n);
}
What is wrong in my code . The value of i is not changing while calling the function.
.