-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathN-queens.cpp
66 lines (51 loc) · 1.45 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
// 51. Leetcode {HARD} [Backtracking, Recursion]
#include <bits/stdc++.h>
using namespace std;
class Solution
{
private:
bool isSafe(int row, int col, vector<string> &grid, int n)
{
// check if the column had a queen before.
for (int i = 0; i != row; ++i)
if (grid[i][col] == 'Q')
return false;
// check if the 45° diagonal had a queen before.
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; --i, --j)
if (grid[i][j] == 'Q')
return false;
// check if the 135° diagonal had a queen before.
for (int i = row - 1, j = col + 1; i >= 0 && j < n; --i, ++j)
if (grid[i][j] == 'Q')
return false;
return true;
}
public:
void solve(int row, vector<string> &grid, vector<vector<string>> &ans, int n)
{
// BASE COND'N
if (row == n)
{
ans.push_back(grid);
return;
}
for (int col = 0; col < n; col++)
{
if (isSafe(row, col, grid, n))
{
grid[row][col] = 'Q';
solve(row + 1, grid, ans, n);
grid[row][col] = '.';
}
}
}
public:
vector<vector<string>> solveNQueens(int n)
{
vector<vector<string>> ans;
vector<string> grid(n, string(n, '.'));
// string s(n,'.')
solve(0, grid, ans, n);
return ans;
}
};