forked from kamyu104/LintCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn-queens.cpp
55 lines (48 loc) · 1.52 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
// Time: O(n * n!)
// Space: O(n)
class Solution {
public:
/**
* Get all distinct N-Queen solutions
* @param n: The number of queens
* @return: All distinct solutions
* For example, A string '...Q' shows a queen on forth position
*/
vector<vector<string>> solveNQueens(int n) {
vector<int> placement(n);
vector<vector<string>> result;
NQueensHelper(n, 0, &placement, &result);
return result;
}
void NQueensHelper(int n, int row, vector<int>* col_placement,
vector<vector<string>>* result) {
if (row == n) {
result->emplace_back(CreateOutput(*col_placement));
} else {
for (int col = 0; col < n; ++col) {
(*col_placement)[row] = col;
if (IsFeasible(*col_placement, row)) {
NQueensHelper(n, row + 1, col_placement, result);
}
}
}
}
vector<string> CreateOutput(const vector<int>& col_placement) {
vector<string> sol;
for (int row : col_placement) {
string line(col_placement.size(), '.');
line[row] = 'Q';
sol.emplace_back(line);
}
return sol;
}
bool IsFeasible(const vector<int>& col_placement, int row) {
for (int i = 0; i < row; ++i) {
int diff = abs(col_placement[i] - col_placement[row]);
if (diff == 0 || diff == row - i) {
return false;
}
}
return true;
}
};