Skip to content

Instantly share code, notes, and snippets.

@amankharwal
Created December 22, 2020 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amankharwal/e56cec4d15ba6b73194a351d1370815a to your computer and use it in GitHub Desktop.
Save amankharwal/e56cec4d15ba6b73194a351d1370815a to your computer and use it in GitHub Desktop.
#include<iostream>
using namespace std;
bool isSafe(int** arr, int x, int y, int n){
for(int row=0; row < x; row++){
if(arr[row][y]==1){
return false;
}
}
int row = x;
int col;
while(row >= 0 && col >=0){
if(arr[row][col]==1){
return false;
}
row--;
col--;
}
row = x;
col = y;
while(row >= 0 && col < n){
if(arr[row][col]==1){
return false;
}
row--;
col++;
}
return true;
}
bool nQueen(int** arr, int x, int n){
if(x >= n){
return true;
}
for(int col=0; col < n; col++){
if(isSafe(arr, x, col, n)){
arr[x][col]=1;
if(nQueen(arr, x+1, n)){
return true;
}
arr[x][col] = 0;
}
}
return false;
}
int main(){
int 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;
}
}
if(nQueen(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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment