forked from USPCodeLabSanca/dev.hire-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path51.cpp
More file actions
99 lines (84 loc) · 2.96 KB
/
51.cpp
File metadata and controls
99 lines (84 loc) · 2.96 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
class Solution {
public:
vector<vector<string>> solveNQueens(int n) {
vector<vector<string>> allSolutions;
unordered_set<string> queenPlacement;
vector<string> baseChess;
// Make chess string
for (int i = 0; i < n; i++) {
string str(n, '.');
baseChess.push_back(str);
}
// Perform the algorithm
for (int i = 0; i < n; i++) {
backtracking(0, i, n, n, baseChess, queenPlacement, allSolutions);
}
return allSolutions;
}
string chessVectorToString(vector<string> chess) {
string str = "";
for (int i = 0; i < chess.size(); i++) {
str = str + chess[i] + "\n";
}
return str;
}
void backtracking(int i, int j, int n, int size, vector<string> solution, unordered_set<string>& queenPlacement, vector<vector<string>>& allSolutions) {
// Check if is a valid position
if (solution[i][j] != '.')
return;
// Block positions
// [UP, LEFT] diagonal
for (int k = i, d = j; k >= 0 && d >= 0; k--, d--) {
solution[k][d] = 'X';
}
// [UP, RIGHT] diagonal
for (int k = i, d = j; k >= 0 && d < size; k--, d++) {
solution[k][d] = 'X';
}
// [DOWN, LEFT] diagonal
for (int k = i, d = j; k < size && d >= 0; k++, d--) {
solution[k][d] = 'X';
}
// [DOWN, RIGHT] diagonal
for (int k = i, d = j; k < size && d < size; k++, d++) {
solution[k][d] = 'X';
}
// [UP] position
for (int k = i, d = j; k >= 0; k--) {
solution[k][d] = 'X';
}
// [DOWN] position
for (int k = i, d = j; k < size; k++) {
solution[k][d] = 'X';
}
// [LEFT] position
for (int k = i, d = j; d >= 0; d--) {
solution[k][d] = 'X';
}
// [RIGHT] position
for (int k = i, d = j; d < size; d++) {
solution[k][d] = 'X';
}
solution[i][j] = 'Q';
// Check if found a valid solution
if (n == 1) {
queenPlacement.insert(this->chessVectorToString(solution));
// Change X to .
for (int k = 0; k < solution.size(); k++) {
for (int d = 0; d < solution[k].length(); d++) {
if (solution[k][d] == 'X')
solution[k][d] = '.';
}
}
allSolutions.push_back(solution);
return;
}
// If not, check candidates
for (int x = 0; x < size; x++) {
if ((i + 1) < size && solution[(i + 1)][x] != '.')
continue;
// Continue the search
this->backtracking(i + 1, x, n - 1, size, solution, queenPlacement, allSolutions);
}
}
};