-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn_queens_ii.rs
46 lines (43 loc) · 1.23 KB
/
n_queens_ii.rs
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
///
/// Problem: N-Queens II
///
/// The N-Queens II problem asks for the number of distinct ways to place `n` queens on an `n x n` chessboard **such that no two queens attack each other**.
///
/// **Example 1:**
/// ```plaintext
/// Input: n = 4
/// Output: 2
/// Explanation: There are two distinct solutions.
/// ```
///
/// **Example 2:**
/// ```plaintext
/// Input: n = 1
/// Output: 1
/// ```
///
/// **Constraints:**
/// - `1 <= n <= 9`
///
/// # Solution:
/// - **Time Complexity:** `O(n!)`
/// - **Space Complexity:** `O(n)`
impl Solution {
pub fn total_n_queens(n: i32) -> i32 {
let mut count = 0;
Self::backtrack(n as usize, 0, 0, 0, 0, &mut count);
count
}
fn backtrack(n: usize, row: usize, cols: i32, diag1: i32, diag2: i32, count: &mut i32) {
if row == n {
*count += 1;
return;
}
let mut available_positions = ((1 << n) - 1) & !(cols | diag1 | diag2);
while available_positions != 0 {
let position = available_positions & -available_positions;
available_positions &= available_positions - 1;
Self::backtrack(n, row + 1, cols | position, (diag1 | position) << 1, (diag2 | position) >> 1, count);
}
}
}