|
@@ -136,6 +136,9 @@ mod tests {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+pub type SudokuBoard = [u8; MAX_SIZE as usize];
|
|
|
|
+pub type SudokuPossible = [Possible; MAX_SIZE as usize];
|
|
|
|
+
|
|
#[derive(Debug)]
|
|
#[derive(Debug)]
|
|
pub struct Sudoku {
|
|
pub struct Sudoku {
|
|
pub board: [u8; MAX_SIZE as usize],
|
|
pub board: [u8; MAX_SIZE as usize],
|
|
@@ -351,7 +354,7 @@ impl Sudoku {
|
|
true
|
|
true
|
|
}
|
|
}
|
|
|
|
|
|
- fn calculate_possible(&mut self, solutions: &mut u16) -> bool {
|
|
|
|
|
|
+ fn calculate_possible(&mut self, total_solutions: &mut u16, solutions: &mut Vec<SudokuBoard>) -> bool {
|
|
for idx in 0..MAX_SIZE {
|
|
for idx in 0..MAX_SIZE {
|
|
if self.board[idx as usize] == 0 {
|
|
if self.board[idx as usize] == 0 {
|
|
// Ok, there's a blank here
|
|
// Ok, there's a blank here
|
|
@@ -391,13 +394,18 @@ impl Sudoku {
|
|
|
|
|
|
self.board[idx as usize] = possible;
|
|
self.board[idx as usize] = possible;
|
|
if self.puzzle_complete() {
|
|
if self.puzzle_complete() {
|
|
- *solutions += 1;
|
|
|
|
|
|
+ if *total_solutions < solutions.capacity() as u16 {
|
|
|
|
+ solutions.push(self.board);
|
|
|
|
+ }
|
|
|
|
+ *total_solutions += 1;
|
|
|
|
+ /*
|
|
println!("**SOLUTION**");
|
|
println!("**SOLUTION**");
|
|
self.display();
|
|
self.display();
|
|
println!("***");
|
|
println!("***");
|
|
|
|
+ */
|
|
break;
|
|
break;
|
|
} else {
|
|
} else {
|
|
- if self.calculate_possible(solutions) {
|
|
|
|
|
|
+ if self.calculate_possible(total_solutions, solutions) {
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -416,11 +424,19 @@ impl Sudoku {
|
|
possible: [Possible(0); MAX_SIZE as usize],
|
|
possible: [Possible(0); MAX_SIZE as usize],
|
|
};
|
|
};
|
|
|
|
|
|
- let mut solutions: u16 = 0;
|
|
|
|
- workset.calculate_possible(&mut solutions);
|
|
|
|
|
|
+ let mut total_solutions: u16 = 0;
|
|
|
|
+ let mut solutions = Vec::new();
|
|
|
|
+ solutions.reserve(1);
|
|
|
|
+ workset.calculate_possible(&mut total_solutions, &mut solutions);
|
|
|
|
|
|
// return number of solutions.
|
|
// return number of solutions.
|
|
- solutions
|
|
|
|
|
|
+ if solutions.len() > 0 {
|
|
|
|
+ println!("*** A solution:");
|
|
|
|
+ workset.board = solutions[0];
|
|
|
|
+ workset.display();
|
|
|
|
+ println!("***");
|
|
|
|
+ }
|
|
|
|
+ total_solutions
|
|
}
|
|
}
|
|
|
|
|
|
pub fn make(&mut self) {
|
|
pub fn make(&mut self) {
|