Data update
This commit is contained in:
parent
ed705008a8
commit
0df55f9f24
2196 changed files with 32999 additions and 3075 deletions
|
|
@ -7,7 +7,7 @@ fn main() {
|
|||
}
|
||||
}
|
||||
|
||||
fn permutations<'a, T, I>(collection: I) -> Box<Iterator<Item=LinkedList<T>> + 'a>
|
||||
fn permutations<'a, T, I>(collection: I) -> Box<Iterator<dyn Item=LinkedList<T>> + 'a>
|
||||
where I: 'a + IntoIterator<Item=T> + Clone,
|
||||
T: 'a + PartialEq + Copy + Clone {
|
||||
if collection.clone().into_iter().count() == 0 {
|
||||
|
|
@ -26,7 +26,7 @@ fn permutations<'a, T, I>(collection: I) -> Box<Iterator<Item=LinkedList<T>> + '
|
|||
}
|
||||
|
||||
pub struct NQueens {
|
||||
iterator: Box<Iterator<Item=NQueensSolution>>
|
||||
iterator: Box<Iterator<dyn Item=NQueensSolution>>
|
||||
}
|
||||
|
||||
impl NQueens {
|
||||
|
|
|
|||
29
Task/N-queens-problem/Rust/n-queens-problem-3.rust
Normal file
29
Task/N-queens-problem/Rust/n-queens-problem-3.rust
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
extern crate itertools;
|
||||
|
||||
use itertools::Itertools;
|
||||
|
||||
fn main() {
|
||||
const N: usize = 8;
|
||||
|
||||
let permutations = (0..N).permutations(N);
|
||||
|
||||
let solution_count = permutations
|
||||
.filter(|p| {
|
||||
let mut diag1 = [false; 2 * N - 1];
|
||||
let mut diag2 = [false; 2 * N - 1];
|
||||
|
||||
for (i, &row) in p.iter().enumerate() {
|
||||
if diag1[row + i] || diag2[N - 1 + row - i] {
|
||||
return false; // Queens mutual threat
|
||||
}
|
||||
|
||||
diag1[row + i] = true;
|
||||
diag2[N - 1 + row - i] = true;
|
||||
}
|
||||
|
||||
true // No Queens mutual threat
|
||||
})
|
||||
.count();
|
||||
|
||||
println!("{}", solution_count);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue