-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.rs
64 lines (58 loc) · 1.53 KB
/
main.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
fn main() {
println!("Hello, world!");
}
struct Solution {}
impl Solution {
pub fn total_n_queens(n: i32) -> i32 {
let n = n as usize;
let mut board = vec![vec!['.'; n]; n];
let mut ret = 0;
Self::solve(&mut board, 0, &mut ret);
ret
}
pub fn solve(board: &mut Vec<Vec<char>>, line: usize, ret: &mut i32) {
if line >= board.len() {
*ret += 1;
return
}
for i in 0..board.len() {
if Self::valid(board, line, i) {
board[line][i] = 'Q';
Self::solve(board, line + 1, ret);
board[line][i] = '.';
}
}
}
pub fn valid(board: &Vec<Vec<char>>, i: usize, j: usize) -> bool {
// check column up
for idx in 0..i {
if board[idx][j] == 'Q' { return false }
}
// check diagonal \ up
let (mut idx, mut jdx) = (i as i32 - 1, j as i32 - 1);
while idx >= 0 && jdx >= 0 {
if board[idx as usize][jdx as usize] == 'Q' { return false }
idx -= 1;
jdx -= 1;
}
// check diagonal / up
let (mut idx, mut jdx) = (i as i32 - 1, j + 1);
while idx >= 0 && jdx < board.len() {
if board[idx as usize][jdx] == 'Q' { return false }
idx -= 1;
jdx += 1;
}
true
}
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn basic() {
assert_eq!(
Solution::total_n_queens(4),
2
)
}
}