-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathN_queen_problem.cpp
More file actions
54 lines (49 loc) · 1.02 KB
/
Copy pathN_queen_problem.cpp
File metadata and controls
54 lines (49 loc) · 1.02 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
#include<bits/stdc++.h>
using namespace std;
std::vector<std::vector<int>> ans;
std::vector<int> a;
bool isvalid(int n , int row , int col){
//cheaking for row
for(int i =0;i<n;i++)
if(a[row][i] =='Q')return false;
//cheaking for colom
for(int i =0;i<n;i++)
if(a[i][col] =='Q')return false;
//cheaking for top left to right bottom
for(int i =0;i<n;i++){
if(row+i <n and col+i<n)
if(a[row+i][col+i] == 'Q')return false;
}
for(int i =0;i<n;i++){
if(row-i <n and col-i<n)
if(a[row-i][col-i] == 'Q')return false;
}
// cheaking for top right to bottom left
for(int i =0;i<n;i++){
if(row+i <n and col-i<n)
if(a[row+i][col-i] == 'Q')return false;
}
for(int i =0;i<n;i++){
if(row-i <n and col+i<n)
if(a[row-i][col+i] == 'Q')return false;
}
return true;
}
void N_queen_solution(int n,int row ){
if(row == n){
ans.push_back(a);
return
}
for(int i =0;i<n;i++){
if(isvalid(n,row,i)){
a[row][i] = 'Q';
N_queen_solution(n,row+1);
a[row][i] = '.'
}
}
return ;
}
int main(int argc, char const *argv[])
{
return 0;
}