-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnqueenProblem.cpp
More file actions
68 lines (66 loc) · 1.26 KB
/
nqueenProblem.cpp
File metadata and controls
68 lines (66 loc) · 1.26 KB
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
#include <bits/stdc++.h>
using namespace std;
bool isPos(vector<vector<int>> board,int col,int row){
int n=board.size();
if(col==0){
return true;
}
//row check
for(int c=0;c<col;++c){
if(board[row][c]){
return false;
}
}
int pos1=row+1;
int pos2=row-1;
int c=col-1;
//upper diagonal
while(pos2>=0&&c>=0){
if(board[pos2][c]){
return false;
}
pos2--;
c--;
}
c=col-1;
//lower diagonal
while(pos1<n&&c>=0){
if(board[pos1][c]){
return false;
}
pos1++;
c--;
}
return true;
}
bool nQueen(vector<vector<int>> &board,int col,int n){
if(col==n){
//possible solution found
return true;
}
for(int i=0;i<n;++i){
board[i][col]=1;
if(isPos(board,col,i) && nQueen(board,(col+1),n)){
return true;
}
//backtrack
board[i][col]=0;
}
return false;
}
int main(){
vector<vector<int>> board={
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0}
};
nQueen(board,0,4);
for(auto el:board){
for(auto c:el){
cout<<c<<" ";
}
cout<<endl;
}
return 0;
}