Created
November 25, 2019 23:16
-
-
Save ignaciomosca/c70dffeaa4d297b9b372616344597b88 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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