-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path51.n-queens.cpp
More file actions
54 lines (46 loc) · 1.38 KB
/
51.n-queens.cpp
File metadata and controls
54 lines (46 loc) · 1.38 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
class Solution {
private:
vector<vector<int>> grid;
vector<vector<string>> result;
vector<string> current;
int n;
void changeGrid(int y, int x, int amount) {
int startVal = grid[y][x];
for(int i = 0; i < n; i++) {
grid[y][i] += amount;
grid[i][x] += amount;
int up = y - i;
int down = y + i;
int right = x + i;
int left = x - i;
if(up >= 0 && right < n) grid[up][right] += amount;
if(up >= 0 && left >= 0) grid[up][left] += amount;
if(down < n && right < n) grid[down][right] += amount;
if(down < n && left >= 0) grid[down][left] += amount;
}
grid[y][x] = startVal + amount;
}
void dfs(int row) {
if(row >= n) {
vector<string> res = current;
result.push_back(res);
return;
}
for(int x = 0; x < n; x++) {
if(grid[row][x] > 0) continue;
changeGrid(row, x, 1);
current[row][x] = 'Q';
dfs(row + 1);
changeGrid(row, x, -1);
current[row][x] = '.';
}
}
public:
vector<vector<string>> solveNQueens(int size) {
n = size;
grid.resize(n, vector<int>(n, 0));
current.resize(n, string(n, '.'));
dfs(0);
return result;
}
};