Here in this solution i am using backtracking approach. In this problem we have to place a queens if possible in each row such that it is at safe position.
safe position here means:

there is no other queen in that same row there is no other queen in that same coloumn. there is no other queen present diagnoaly corresponding to the current position.

for checking these three things i have made a funciton issafe();
and for solving the main problem recursively(using backtracking) i have made a solve function.

#include<bits/stdc++.h> using namespace std; int ar[11][11]; int n; bool issafe(int row,int col) { // coloumn check int count=0; for(int k=0;k<row;k++) if(ar[k][col]==1) return false; //upper right diagnoal int k=row; int l=col; while(k>=0&&l<n){ if(ar[k][l]==1) return false; k--; l++; } //upper left diagnoal k=row; l=col; while(k>=0&&l>=0) { if(ar[k][l]==1) return false; k--; l--; } return true; } bool solve(int row) { if(row>=n) return true; for(int i=0;i<n;i++) { if(issafe(row,i)) {ar[row][i]=1; if(solve(row+1)) return true; ar[row][i]=0; } } return false; } void print1() { for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { cout<<ar[i][j]<<" "; } cout<<endl; } } main() { cin>>n; for(int i=0;i<n;i++) for(int j=0;j<n;j++) ar[i][j]=0; if(solve(0)) print1(); else cout<<"Not possible"; }