-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathN-queens2.cpp
62 lines (49 loc) · 1.34 KB
/
N-queens2.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
// 52. Leetcode [Backtracking, Recursion]
#include <bits/stdc++.h>
using namespace std;
class Solution
{
private:
bool isSafe(int row, int col, vector<vector<bool>> &grid, int n)
{
// check if the column had a queen before.
for (int i = 0; i != row; ++i)
if (grid[i][col] == 1)
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] == 1)
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] == 1)
return false;
return true;
}
public:
int solve(int row, vector<vector<bool>> &grid, int n)
{
// BASE COND'N
if (row == n)
{
return 1;
}
int count = 0;
for (int col = 0; col < n; col++)
{
if (isSafe(row, col, grid, n))
{
grid[row][col] = 1;
count += solve(row + 1, grid, n);
grid[row][col] = 0;
}
}
return count;
}
public:
int totalNQueens(int n)
{
vector<vector<bool>> grid(n, vector<bool>(n, 0));
return solve(0, grid, n);
}
};