exactly same code written by sir still no output is coming
#include
using namespace std;
bool isSafe(int board[][10], int i, int j, int n){
//you can check for column
for(int row=0;row<i;row++){
if(board[row][j]==1){
return false;
}
}
//you can check for left diagonl
int x = i;
int y = j;
while(x>=0 && y>=0)
{
if(board[x][y]==1){
return false;
}
x--;
y--;
}
//you can check for right diagonal
x = i;
y = j;
while(x>=0 && y<n)
{
if(board[x][y]==1){
return false;
}
x--;
y++;
}
//the position is now safe, column and diagonals
return true;
}
bool solveNQueen(int board[][10],int i, int n){
//base case
if(i==n){
//you hav successfully place queens in n rows( 0,…,n-1);
//print the board;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(board[i][j]==1){
cout<<“Q”;
}
else{
cout<<"_ ";
}
}
cout<<endl;
}
return true;
}
//rec case
//try to place the queen in the current row and call on the remaining mart which will b done by recursion
for(int j=0;j<n;j++){
//i hav to check if i,j th position is safe to place the queen or not
if(isSafe(board,i,j,n)){
//place the queen - assuming i,j is the correct position
board[i][j]=1;
bool nextQueenRakhPaye = solveNQueen(board,i+1,n);
if(nextQueenRakhPaye){
return true;
}
//means i,j if not the correct position - Assumption is wrong
board[i][j]=0;//backtrack
}
}
//you hav tried all positions in a current row but couldn't place the queen
return true;
}
int main(){
int n;
cin>>n;
int board[10][10] = {0};
solveNQueen(board,0,n);
return 0;}
