Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. package programming.set7.sudoku;
  2.  
  3. /**
  4. * Implements a simple backtracking-based Sudoku solver. We start at the
  5. * top-leftmost non-empty cell and start filling it with numbers. If we find a
  6. * valid number to put in there, we assume it to be fixed and do the same with
  7. * the next non-empty field. As soon as we discover that the instance is not
  8. * solvable with our number, we immediately change to the next valid number and
  9. * do it all over.
  10. */
  11. public class SudokuSolver {
  12.  
  13. /** The Sudoku to be solved. */
  14. private Sudoku sudoku = null;
  15.  
  16. /**
  17. * Creates a new instance to solve the given Sudoku.
  18. *
  19. * @param s the Sudoku to solve.
  20. */
  21. public SudokuSolver(Sudoku s) {
  22. // Make a copy of the Sudoku
  23. sudoku = new Sudoku();
  24.  
  25. for (int row = 0; row < 9; row++) {
  26. for (int col = 0; col < 9; col++) {
  27. sudoku.setValueAt(row, col, s.getValueAt(row, col));
  28. }
  29. }
  30. }
  31.  
  32. /**
  33. * Attempts to solve the Sudoku.
  34. */
  35. public void solve() {
  36. solveFrom(0);
  37. }
  38.  
  39. /**
  40. * Solves the Sudoku by trying values for the cell with the given index and
  41. * seeing what happens. The cells are numbered starting with 0 for the top-left
  42. * cell and iterating in row-major order.
  43. *
  44. * @param cell the cell index.
  45. * @return {@code true} if we found a solution, {@code false} otherwise.
  46. */
  47. private boolean solveFrom(int cell) {
  48. // Calculate the row and column from the cell index
  49. int col = cell % 9;
  50. int row = cell / 9;
  51.  
  52. // If we're out of range, we're done
  53. if (row > 8) {
  54. return sudoku.isValid();
  55. }
  56.  
  57. // If the Sudoku is currently invalid, don't even start
  58. if (!sudoku.isValid()) {
  59. return false;
  60. }
  61.  
  62. if (sudoku.getValueAt(row, col) != Sudoku.EMPTY) {
  63. // The cell is not empty, so jump to the next
  64. return solveFrom(cell + 1);
  65. } else {
  66. // The cell is empty, so start trying things
  67. for (int number = 1; number <= 9; number++) {
  68. sudoku.setValueAt(row, col, number);
  69.  
  70. // If this call returns true, we've found a solution
  71. if (solveFrom(cell + 1)) {
  72. return true;
  73. }
  74. }
  75.  
  76. sudoku.setValueAt(row, col, Sudoku.EMPTY);
  77.  
  78. // We haven't found a solution
  79. return false;
  80. }
  81. }
  82.  
  83. /**
  84. * Checks whether the Sudoku was solved or not. A Sudoku is solved if it is
  85. * valid and has no empty cells.
  86. *
  87. * @return {@code true} if the Sudoku was already solved.
  88. */
  89. public boolean isSolved() {
  90. if (!sudoku.isValid()) {
  91. return false;
  92. }
  93.  
  94. // Check if all cells are non-empty
  95. for (int row = 0; row < 9; row++) {
  96. for (int col = 0; col < 9; col++) {
  97. if (sudoku.getValueAt(row, col) == NumberBoard.EMPTY) {
  98. return false;
  99. }
  100. }
  101. }
  102.  
  103. return true;
  104. }
  105.  
  106. /**
  107. * Returns the solved Sudoku instance.
  108. *
  109. * @return the solved Sudoku, or {@code null} if the Sudoku wasn't solved (yet).
  110. */
  111. public Sudoku getSolution() {
  112. return isSolved() ? sudoku : null;
  113. }
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement