-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnqueen.cpp
86 lines (82 loc) · 2.19 KB
/
nqueen.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
bool isSafe(int** arr, int x, int y, int n){
//for every column we are chcecking a row
for(int row=0;row<x;row++){
if(arr[row][y]==1){
//if queen is placed
return false;
}
}
//checking for diagnols
int row = x;
int col = y;
//left diagnol
//so that row don't get out of bound
while(row>=0 && col>=0){
if(arr[row][col]==1){
//if queen is present
return false;
}
row--;
col--;
}
//right diagnol
row = x;
col = y;
//checking column constraints so that it dont get out of bound
while(row>=0 && col<n){
if(arr[row][col]==1){
return false;
}
row--;
col++;
}
//if nothing is return false we will return true
return true;
}
bool nQueen(int** arr,int x,int n){
//x-row
//base condn i.e all n queens are placed
if(x>=n){
return true;
}
for(int col=0;col<n;col++){
//before placing the queen we are checking whether that pos is safe or not
if(isSafe(arr,x,col,n)){
//then placing queen on that pos
arr[x][col]=1;
//checking for next row x+1
//matlab is row pe rakhne ke baad hum next rows pe queen ko
//place kar sakte hai to check karo varna jispe place kara tha humne upar usko vapis se 0 karo aur ab voh next row ke liye check karega
if(nQueen(arr,x+1,n)){
return true;
}
//backtracking to previous pos and marking that 0
arr[x][col]=0;
}
}
//after checking all the columns
return false;
}
int main()
{
int n;
cin>>n;
int** arr = new int*[n]; //2d array
for(int i=0;i<n;i++){
arr[i] = new int[n];
for(int j=0;j<n;j++){
arr[i][j]=0;//initialising it to 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;
}