-
Notifications
You must be signed in to change notification settings - Fork 506
/
Copy pathN-Queens II.cpp
88 lines (74 loc) · 1.54 KB
/
N-Queens II.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
N-Queens II
===========
The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other.
Given an integer n, return the number of distinct solutions to the n-queens puzzle.
Example 1:
Input: n = 4
Output: 2
Explanation: There are two distinct solutions to the 4-queens puzzle as shown.
Example 2:
Input: n = 1
Output: 1
Constraints:
1 <= n <= 9
*/
class Solution
{
public:
bool isSafe(vector<vector<int>> &board, int r, int c)
{
int n = board.size();
if (r < 0 || c < 0 || r >= n || c >= n)
return false;
// left side
for (int i = 0; i <= c; ++i)
{
if (board[r][i] == 1)
return false;
}
// up side
for (int i = 0; i <= r; ++i)
{
if (board[i][c] == 1)
return false;
}
// top left diagonal
for (int i = r, j = c; i >= 0 && j >= 0; j--, i--)
{
if (board[i][j] == 1)
return false;
}
// top right diagonal
for (int i = r, j = c; i >= 0 && j < n; j++, i--)
{
if (board[i][j] == 1)
return false;
}
return true;
}
void solve(int n, vector<vector<int>> &board, int r, int &ans)
{
if (r == n)
{
ans++;
return;
}
for (int c = 0; c < n; ++c)
{
if (isSafe(board, r, c))
{
board[r][c] = 1;
solve(n, board, r + 1, ans);
board[r][c] = 0;
}
}
}
int totalNQueens(int n)
{
int ans = 0;
vector<vector<int>> board(n, vector<int>(n, 0));
solve(n, board, 0, ans);
return ans;
}
};