Bugz 9 місяців тому
батько
коміт
48faebeaf7
1 змінених файлів з 22 додано та 6 видалено
  1. 22 6
      sudoku/src/sudoku.rs

+ 22 - 6
sudoku/src/sudoku.rs

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