-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathN_Queen_Problem.cpp
More file actions
67 lines (65 loc) · 1.53 KB
/
Copy pathN_Queen_Problem.cpp
File metadata and controls
67 lines (65 loc) · 1.53 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
// Complexity = O(n!)
#include<iostream>
#include<vector>
using namespace std;
bool is_safe(vector<string>&board,int row,int col,int n){
for(int i=0;i<n;i++){
if(board[i][col] == 'Q'){
return false;
}
}
for(int j=0;j<n;j++){
if(board[row][j]=='Q'){
return false;
}
}
for(int i=row,j=col;i>=0 && j>=0;i--,j--){
if(board[i][j] == 'Q'){
return false;
}
}
for(int i=row,j=col;i>=0 && j<n;i--,j++){
if(board[i][j]=='Q'){
return false;
}
}
return true;
}
void nQueens(vector<string>&board,int row,int n,vector<vector<string>>&ans){
if(row == n){
ans.push_back({board});
return;
}
for(int j=0;j<n;j++){
if(is_safe(board,row,j,n)){
board[row][j] ='Q';
nQueens(board,row+1,n,ans);
board[row][j]='.';
}
}
}
vector<vector<string>> solve_nQueeens(int n){
vector<string>board(n,string(n,'.'));
vector<vector<string>> ans;
nQueens(board,0,n,ans);
return ans;
}
int main(){
vector<vector<string>>a = solve_nQueeens(4);
int x= 1;
for (vector<string> board : a) {
for (string s : board) {
for(char ch:s){
cout << ch;
if(ch == 'Q'){
cout<<x;
x++;
}
}
cout<<endl;
}
cout << endl;
x=1;
}
return 0;
}