fn check()は逆順にして、左下もしくは右下にあるかチェックしてる?
なんとなくわかるが、完全に理解していないのが辛いところ。。
use std::collections::VecDeque; static N: usize = 8; fn check(x: usize, mut col: VecDeque<usize>)-> bool { col.make_contiguous().reverse(); let mut i = 0; for row in col { if ((x + i + 1) == row) || ((x as i32- i as i32 - 1) == row.try_into().unwrap()) { return false } i = i + 1; } return true } fn search(mut col: VecDeque<usize>){ if col.clone().len() == N { println!("{:?}", &col); } for i in 0..N { if !col.contains(&i) { // println!("{:?}", &col); // println!("{}", i); if check(i, col.clone()) == true { col.push_back(i); search(col.clone()); col.pop_front(); } } } } fn main() { let col: VecDeque<usize> = VecDeque::new(); search(col); }
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.23s
Running `target/debug/rust`
[4, 1, 3, 5, 7, 2, 0, 6]
[1, 3, 5, 7, 2, 0, 6, 4]
[5, 2, 4, 6, 0, 3, 1, 7]
[2, 4, 6, 0, 3, 1, 7, 5]
[1, 3, 5, 7, 2, 0, 6, 4]
…