Skip to content

Commit c9dbdf1

Browse files
committed
Runtime: 0 ms (Top 100.0%) | Memory: 2.10 MB (Top 64.29%)
1 parent 597693c commit c9dbdf1

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Runtime: 0 ms (Top 100.0%) | Memory: 2.10 MB (Top 64.29%)
2+
3+
impl Solution {
4+
pub fn total_n_queens(n: i32) -> i32 {
5+
if n < 1 { return 0; }
6+
7+
let mut result = 0;
8+
Self::_dfs(n, &mut result, 0, 0, 0, 0);
9+
result
10+
}
11+
12+
pub fn _dfs(n: i32, result: &mut i32, row: i32, col: i32, pie: i32, na: i32) {
13+
if row >= n {
14+
*result += 1;
15+
return;
16+
}
17+
18+
let mut bits = (!(col | pie | na)) & ((1 << n) - 1);
19+
while bits != 0 {
20+
let p = bits & -bits;
21+
Self::_dfs(n, result, row + 1, col | p, (pie | p) << 1, (na | p) >> 1);
22+
bits = bits & (bits - 1);
23+
}
24+
}
25+
26+
}

0 commit comments

Comments
 (0)