#include<iostream>
using namespace std;
bool isqueen(int** arr,int x,int y,int n)
{
/* To check that already
queen is presence or not in the column */
for(int row=0; row < x ; row++)
{ if(arr[row][y]==1)
{return false;}
}
/*To check that already
queen is presence or not in the first diogonal */
int row=x;
int col=y;
while(row>=0 && col>=0)
{ if(arr[row][col]==1)
{return false;}
row--;
col--;
}
row=x;
col=y;
/*To check that already
queen is presence or not in the secound diogonal*/
while(row>=0 && col<n)
{ if(arr[row][col]==1)
{return false;}
row--;
col++;
}
return true;
}
bool arrangequeen(int** arr ,int x,int n)
{ if( x >= n)
{return true;}
for(int col=0;col<n;col++)
{ if(isqueen( arr,x,col,n))
{arr[x][col]=1;
if(arrangequeen(arr,x+1,n))
{ return true;}
arr[x][col]=0; // backtracking
}
}
return false;
}
int main()
{
int n;
cout<<" enter row or column n= ";
cin>>n;
int** arr = new int *[n];
for(int i=0;i<n;i++)
{arr[i]=new int[n];
for(int j=0;j<n;j++)
{arr[i][j]=0;}
}
cout<<endl<<"1 show position of queem"<<endl;
if(arrangequeen(arr,0,n))
for(int i=0;i<n;i++)
{for(int j=0;j<n;j++)
{cout<<arr[i][j]<<" ";}
cout<<endl;
}
return 0; }