-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path17_chessboard_and_queens.rs
43 lines (36 loc) · 1.02 KB
/
17_chessboard_and_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
use std::collections::HashSet as HS;
fn main() {
let board: Vec<Vec<char>> = std::io::read_to_string(std::io::stdin())
.unwrap()
.lines()
.map(|s| s.chars().collect())
.collect();
let (mut col, mut pos_diag, mut neg_diag) = (HS::new(), HS::new(), HS::new());
println!(
"{}",
queens(&board, 0, &mut col, &mut pos_diag, &mut neg_diag)
);
}
fn queens(
board: &Vec<Vec<char>>,
r: i8,
col: &mut HS<i8>,
pos_diag: &mut HS<i8>,
neg_diag: &mut HS<i8>,
) -> i8 {
let mut res = 0;
if r == 8 { return 1; }
for c in 0..8 {
if board[r as usize][c as usize] == '*'
|| col.contains(&c)
|| pos_diag.contains(&(r + c))
|| neg_diag.contains(&(r - c))
{
continue;
}
col.insert(c); pos_diag.insert(r + c); neg_diag.insert(r - c);
res += queens(board, r + 1, col, pos_diag, neg_diag);
col.remove(&c); pos_diag.remove(&(r + c)); neg_diag.remove(&(r - c));
}
res
}