-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn_queens.rs
83 lines (76 loc) · 2.03 KB
/
n_queens.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
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
///
/// Problem: N-Queens
///
/// The N-Queens problem involves placing `n` queens on an `n x n` chessboard such that:
/// - No two queens threaten each other.
/// - Queens cannot share the same row, column, or diagonal.
///
/// **Example 1:**
/// ```plaintext
/// Input: n = 4
/// Output:
/// [[".Q..", // Solution 1
/// "...Q",
/// "Q...",
/// "..Q."],
///
/// ["..Q.", // Solution 2
/// "Q...",
/// "...Q",
/// ".Q.."]]
/// ```
///
/// **Example 2:**
/// ```plaintext
/// Input: n = 1
/// Output: [["Q"]]
/// ```
///
/// **Constraints:**
/// - `1 <= n <= 9`
///
/// # Solution:
/// - **Time Complexity:** `O(n!)`
/// - **Space Complexity:** `O(n)`
use std::collections::HashSet;
impl Solution {
pub fn solve_n_queens(n: i32) -> Vec<Vec<String>> {
let mut result = Vec::new();
let mut board = vec![vec!['.'; n as usize]; n as usize];
let mut cols = HashSet::new();
let mut diag1 = HashSet::new();
let mut diag2 = HashSet::new();
Self::backtrack(0, n as usize, &mut board, &mut cols, &mut diag1, &mut diag2, &mut result);
result
}
fn backtrack(
row: usize,
n: usize,
board: &mut Vec<Vec<char>>,
cols: &mut HashSet<usize>,
diag1: &mut HashSet<i32>,
diag2: &mut HashSet<i32>,
result: &mut Vec<Vec<String>>,
) {
if row == n {
result.push(board.iter().map(|r| r.iter().collect()).collect());
return;
}
for col in 0..n {
let d1 = row as i32 - col as i32;
let d2 = row as i32 + col as i32;
if cols.contains(&col) || diag1.contains(&d1) || diag2.contains(&d2) {
continue;
}
board[row][col] = 'Q';
cols.insert(col);
diag1.insert(d1);
diag2.insert(d2);
Self::backtrack(row + 1, n, board, cols, diag1, diag2, result);
board[row][col] = '.';
cols.remove(&col);
diag1.remove(&d1);
diag2.remove(&d2);
}
}
}