My solution is correct but question is asking to solve with void recursive function

#include
using namespace std;

int c=0;
bool isSafe(int board[][10], int i, int j, int n) // this checks if left and right diagonal and vertical up is safeor not
{
for (int row = 0; row < i; row++)
{
if (board[row][j] == 1)
{
return false;
}
}
int x = i;
int y = j;

while (x >= 0 && y >= 0)
{
    if (board[x][y] == 1)
    {
        return false;
    }

    x--;
    y--;
}

x = i, y = j;

while (x >= 0 && y < n)
{
    if (board[x][y] == 1)
    {
        return false;
    }

    x--;
    y++;
}

return true;

}

bool solveNqueen(int board[][10], int i, int n)
{
if (i == n)
{
// print sol
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (board[i][j] == 1)
{
cout<<"{"<<i+1<<"-"<<j+1<<"}"<<" ";
}
}
}
c++;
cout << " ";

    return false;
}

for (int j = 0; j < n; j++)
{
    if (isSafe(board, i, j, n))
    {
        board[i][j] = 1;
        bool nextQueen = solveNqueen(board, i + 1, n);
        if (nextQueen)
        {
            return true;
        }
        board[i][j] = 0;
    }
}
return false;

}

int main()
{
int n;
cin >> n;

int board[10][10] = {0};

solveNqueen(board, 0, n);

cout<<endl<<c;
return 0;
}

Hi @anandsingh3210,

You could change your solveNqueen function like this,

void solveNqueen(int board[][10], int i, int n)
{
    if (i == n)
    {
        // print the grid
        return;    
    }

    for (int j = 0; j < n; j++)
    {
        if (isSafe(board, i, j, n))
        {
            board[i][j] = 1;
            solveNqueen(board, i + 1, n);
            board[i][j] = 0;
        }
    }
}

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.