Skip to content

Instantly share code, notes, and snippets.

@ignaciomosca
Created November 25, 2019 23:16
Show Gist options
  • Save ignaciomosca/c70dffeaa4d297b9b372616344597b88 to your computer and use it in GitHub Desktop.
Save ignaciomosca/c70dffeaa4d297b9b372616344597b88 to your computer and use it in GitHub Desktop.
pub fn solution<'a>(
board_stack: &mut VecDeque<(Board, Vec<ChessPiece>)>,
solutions: &'a mut HashSet<Board>,
tested_configurations: &mut HashSet<Board>,
) -> &'a HashSet<Board> {
while !board_stack.is_empty() {
let (board, pieces) = board_stack.pop_back().unwrap();
for row in 1..=board.m {
for col in 1..=board.n {
let new_piece = Piece {
row,
col,
piece: pieces[0],
};
if board.is_safe(new_piece) {
let new_board = board.place(new_piece);
if pieces.len() != 1 {
if tested_configurations.insert(new_board.clone()) {
let next_board = new_board.clone();
let tail = Vec::from_iter(pieces[1..pieces.len()].iter().cloned());
board_stack.push_front((next_board, tail));
}
} else {
solutions.insert(new_board);
}
}
}
}
}
solutions
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment