Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.07 KB | None | 0 0
  1. /**
  2. * Class that solves the Asterisk Sudoku.
  3. * Prints the number of solutions of a Sudoku if there are multiple. If there is only a single solution, prints this solution instead.
  4. * <p>
  5. * by Eric Abraham, ID: 1408828
  6. * and Ilke Yilmaz, ID: 1410067
  7. * as group 21
  8. * 30/09/2019
  9. */
  10. class SudokuSolver {
  11.  
  12. int SUDOKU_SIZE = 9; // Size of the grid.
  13. int SUDOKU_MIN_NUMBER = 1; // Minimum digit to be filled in.
  14. int SUDOKU_MAX_NUMBER = 9; // Maximum digit to be filled in.
  15. int SUDOKU_BOX_DIMENSION = 3; // Dimension of the boxes (sub-grids that should contain all digits).
  16. boolean[] asteriskArray = new boolean[10]; //array used to determine whether the value at position has already been used
  17.  
  18. int[][] grid = new int[][]{ // The puzzle grid; 0 represents empty.
  19. {0, 9, 0, 7, 3, 0, 4, 0, 0}, // One solution.
  20. {0, 0, 0, 0, 0, 0, 5, 0, 0},
  21. {3, 0, 0, 0, 0, 6, 0, 0, 0},
  22.  
  23. {0, 0, 0, 0, 0, 2, 6, 4, 0},
  24. {0, 0, 0, 6, 5, 1, 0, 0, 0},
  25. {0, 0, 6, 9, 0, 7, 0, 0, 0},
  26.  
  27. {5, 8, 0, 0, 0, 0, 0, 0, 0},
  28. {9, 0, 0, 0, 0, 3, 0, 2, 5},
  29. {6, 0, 3, 0, 0, 0, 8, 0, 0},
  30. };
  31. char[][] charAfter = new char[][]{ //used to properly print grid
  32. {' ', ' ', '|', ' ', ' ', '|', ' ', ' ', '|'},
  33. {' ', ' ', '|', '>', '<', '|', ' ', ' ', '|'},
  34. {' ', '>', '|', ' ', ' ', '|', '<', ' ', '|'},
  35.  
  36. {' ', ' ', '|', ' ', ' ', '|', ' ', ' ', '|'},
  37. {'>', '<', '|', '>', '<', '|', '>', '<', '|'},
  38. {' ', ' ', '|', ' ', ' ', '|', ' ', ' ', '|'},
  39.  
  40. {' ', '>', '|', ' ', ' ', '|', '<', ' ', '|'},
  41. {' ', ' ', '|', '>', '<', '|', ' ', ' ', '|'},
  42. {' ', ' ', '|', ' ', ' ', '|', ' ', ' ', '|'},
  43. };
  44. int[][] solution = new int[SUDOKU_SIZE][SUDOKU_SIZE]; //solution grid
  45.  
  46. int solutionCounter = 0; // Solution counter
  47.  
  48. // Is there a conflict when we fill in d at position (r, c)?
  49. boolean givesConflict(int r, int c, int d) {
  50. // TODO
  51. if (rowConflict(r, d) || columnConflict(c, d) || boxConflict(r, c, d) || asteriskConflict(r, c, d)) {
  52. return true;
  53. }
  54. return false;
  55. }
  56.  
  57. // Is there a conflict when we fill in d in row r?
  58. boolean rowConflict(int r, int d) {
  59. // TODO
  60. for (int i = 0; i < SUDOKU_SIZE; i++)
  61. if (grid[r][i] == d) {
  62. return true;
  63. }
  64. return false;
  65.  
  66. }
  67.  
  68. // Is there a conflict in column c when we fill in d?
  69. boolean columnConflict(int c, int d) {
  70. for (int i = 0; i < SUDOKU_SIZE; i++)
  71. if (grid[i][c] == d) {
  72. return true;
  73. }
  74. return false;
  75. }
  76.  
  77. // Is there a conflict in the box at (r, c) when we fill in d?
  78. boolean boxConflict(int r, int c, int d) {
  79. for (int i = r / 3 * 3; i < r / 3 * 3 + 3; i++)
  80. for (int j = c / 3 * 3; j < c / 3 * 3 + 3; j++)
  81. if (grid[i][j] == d)
  82. return true;
  83. return false;
  84. }
  85.  
  86. void initializeAsteriskArray() {
  87. for (int i = SUDOKU_MIN_NUMBER; i <= SUDOKU_MAX_NUMBER; i++)
  88. asteriskArray[i] = false;
  89. for (int i = 0; i < SUDOKU_SIZE; i++)
  90. for (int j = 0; j < SUDOKU_SIZE; j++)
  91. if (isAsteriskPosition(i, j))
  92. asteriskArray[grid[i][j]] = true;
  93. }
  94.  
  95. boolean isAsteriskPosition(int r, int c) {
  96. if (r == 2 && (c == 2 || c == 6))
  97. return true;
  98. if (r == 4 && (c == 1 || c == 4 || c == 7))
  99. return true;
  100. if (r == 6 && (c == 2 || c == 6))
  101. return true;
  102. if (r == 7 && c == 4)
  103. return true;
  104. if (r == 1 && c == 4)
  105. return true;
  106. return false;
  107. }
  108.  
  109. boolean asteriskConflict(int r, int c, int d) {
  110. initializeAsteriskArray(); //refreshes AsteriskArray in order to properly determine conflict
  111. if (asteriskArray[d] && isAsteriskPosition(r, c))
  112. return true;
  113. return false;
  114. }
  115.  
  116. // Finds the next empty square (in "reading order").
  117. int[] findEmptySquare() {
  118. // TODO
  119. for (int i = 0; i < SUDOKU_SIZE; i++)
  120. for (int j = 0; j < SUDOKU_SIZE; j++)
  121. if (grid[i][j] == 0)
  122. return new int[]{i, j};
  123. return new int[]{-1, -1};
  124. }
  125.  
  126. void copyGrid() { //copies grid, ready to be printed as solution
  127.  
  128. for (int i = 0; i < SUDOKU_SIZE; i++)
  129. for (int j = 0; j < SUDOKU_SIZE; j++)
  130. solution[i][j] = grid[i][j];
  131. }
  132.  
  133. // Find all solutions for the grid, and stores the final solution.
  134. void solve() {
  135. int[] index = findEmptySquare(); //searches for next empty space
  136. if (index[0] != -1 && index[1] != -1) {
  137. for (int i = SUDOKU_MIN_NUMBER; i <= SUDOKU_MAX_NUMBER; i++)
  138. if (!givesConflict(index[0], index[1], i)) {
  139. grid[index[0]][index[1]] = i;
  140. solve();
  141. grid[index[0]][index[1]] = 0; //when solve 'returns' we are done with our current i of the for
  142. }
  143. } else {
  144. solutionCounter++; //if there is no empty space, we found a solution
  145. if (solutionCounter == 1) {
  146. copyGrid();
  147. }
  148. }
  149. }
  150.  
  151. // Print the sudoku grid.
  152. void print() {
  153. System.out.println("+-----------------+");
  154. for (int i = 0; i < 9; i++) {
  155. System.out.print("|");
  156. for (int j = 0; j < 9; j++) {
  157. System.out.print(solution[i][j]);
  158. System.out.print(charAfter[i][j]);
  159. }
  160.  
  161. System.out.print("\n");
  162. if (i == 2 || i == 5) {
  163. System.out.println("+-----------------+");
  164. }
  165. }
  166. System.out.println("+-----------------+");
  167.  
  168. }
  169.  
  170. // Run the actual solver.
  171. void solveIt() {
  172. solve();
  173. if (solutionCounter == 1)
  174. print();
  175. else
  176. System.out.println(solutionCounter);
  177. }
  178.  
  179. public static void main(String[] args) {
  180. (new SudokuSolver()).solveIt();
  181. }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement