From 1a9d996416691ca17319fa08b5b648e6ab27ab4b Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Mon, 6 Jun 2022 19:51:29 +0100 Subject: [PATCH] n-queens-ii --- n-queens-ii/sol.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 n-queens-ii/sol.rs diff --git a/n-queens-ii/sol.rs b/n-queens-ii/sol.rs new file mode 100644 index 0000000..3563584 --- /dev/null +++ b/n-queens-ii/sol.rs @@ -0,0 +1,29 @@ +// TODO : Tidy up?? This is quite the unreadable mess at the moment +impl Solution { + pub fn search(board: Vec>) -> i32 { + if board.len() == 1 { + return board[0].len(); + } + board[0].iter() + .map(|¤t| + Solution::search( + board[1..].iter() + .enumerate() + .map(|(idx, row)| { + row.iter() + .filter_map(|n| if *n != current && + *n != current - idx as i32 - 1 && + *n != current + idx as i32 + 1 { + Some(*n) + } else { None }) + .collect() + }) + .collect() + ) + ) + .sum() + } + pub fn total_n_queens(n: i32) -> Vec> { + Solution::search(vec![(0..n).collect(); n as usize]) + } +} \ No newline at end of file