-
Notifications
You must be signed in to change notification settings - Fork 0
/
n_queens.rs
66 lines (62 loc) · 1.67 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
#![allow(dead_code)]
pub fn solve_n_queens(n: i32) -> Vec<Vec<String>> {
let mut board = vec![vec!['.'; n as usize]; n as usize];
let mut res = Vec::new();
let mut posdiag = vec![false; 2 * n as usize - 1];
let mut negdiag = vec![false; 2 * n as usize - 1];
let mut cols = vec![false; n as usize];
backtrack(
0,
&mut board,
&mut res,
&mut posdiag,
&mut negdiag,
&mut cols,
);
res
}
fn backtrack(
row: i32,
board: &mut Vec<Vec<char>>,
res: &mut Vec<Vec<String>>,
posdiag: &mut Vec<bool>,
negdiag: &mut Vec<bool>,
cols: &mut Vec<bool>,
) {
let n = board.len() as i32;
if row == n {
res.push(board.iter().map(|r| r.iter().collect()).collect());
return;
}
for col in 0..n {
if posdiag[(row + col) as usize]
|| negdiag[(row - col + n - 1) as usize]
|| cols[col as usize]
{
continue;
}
posdiag[(row + col) as usize] = true;
negdiag[(row - col + n - 1) as usize] = true;
cols[col as usize] = true;
board[row as usize][col as usize] = 'Q';
backtrack(row + 1, board, res, posdiag, negdiag, cols);
posdiag[(row + col) as usize] = false;
negdiag[(row - col + n - 1) as usize] = false;
cols[col as usize] = false;
board[row as usize][col as usize] = '.';
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_51() {
assert_eq!(
solve_n_queens(4),
vec![
vec![".Q..", "...Q", "Q...", "..Q."],
vec!["..Q.", "Q...", "...Q", ".Q.."]
]
);
}
}