-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathn_queens.cpp
82 lines (79 loc) · 1.56 KB
/
n_queens.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
#include <vector>
#include <string>
#include <iostream>
using namespace std;
bool isValid(vector<int> &solution, int depth, int val){
for (int j = 0; j < solution.size(); ++j){
if (solution[j] == val || j-depth == solution[j]-val || j-depth == val-solution[j]){
return false;
}
}
return true;
}
vector<vector<string>> solveNQueens(int n) {
vector<vector<int>> results;
vector<int> solution;
int depth = -1;
int max_depth = n - 1;
vector<int> next_index(n, 0);
while (true){
while (depth < max_depth){
int child_depth = depth + 1;
bool found = false;
while (true){
if (next_index[child_depth] == n){
next_index[child_depth] = 0;
break;
}
int index = next_index[child_depth];
++next_index[child_depth];
if (isValid(solution, child_depth, index)){
solution.push_back(index);
found = true;
break;
}
}
if (!found){
break;
}
++depth;
}
if (depth == max_depth){
results.push_back(solution);
}
if (depth == -1){
break;
}
--depth;
solution.pop_back();
}
vector<vector<string>> rets;
vector<string> i_to_string;
for (int i = 0; i < n; ++i){
string s;
for (int j = 0; j < n; ++j){
if (j == i){
s += "Q";
}else{
s += ".";
}
}
i_to_string.push_back(s);
}
for (int i = 0; i < results.size(); ++i){
vector<string> ret;
for (int j = 0; j < results[i].size(); ++j){
ret.push_back(i_to_string[results[i][j]]);
}
rets.push_back(ret);
}
return rets;
}
int main(){
for (auto i: solveNQueens(4)){
for (auto j: i){
cout << j << endl;
}
}
return 0;
}