Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.12 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Objects;
  4. import java.util.Optional;
  5. import java.util.stream.Collectors;
  6.  
  7.  
  8. class Solution {
  9. public void solveSudoku(char[][] board) {
  10. char[][] x = solve(board);
  11. for(int i = 0; i < board.length; i++)
  12. for(int j = 0; j < board[i].length; j++)
  13. board[i][j] = x[i][j];
  14. }
  15.  
  16.  
  17. public static char[][] solve(char[][] sudoku) {
  18. if (isFullFilled(sudoku))
  19. return sudoku;
  20.  
  21. Optional<Cord> cord1 = findEasiestToFulfill(sudoku);
  22. if (!cord1.isPresent())
  23. return null;
  24.  
  25. Cord cord = cord1.get();
  26. List<Character> possibleNr = getPossibleNr(sudoku, cord.x, cord.y);
  27. if (possibleNr.size() == 0)
  28. return null;
  29.  
  30. List<char[][]> collect = possibleNr
  31. .parallelStream()
  32. .map(x -> solve(sudoku, cord, x))
  33. .filter(Objects::nonNull)
  34. .collect(Collectors.toList());
  35.  
  36. if (collect.size() == 0)
  37. return null;
  38. else
  39. return solve(collect.get(0));
  40. }
  41.  
  42. private static char[][] solve(char[][] sudoku, Cord cord, char x) {
  43. char[][] test = copy(sudoku);
  44. test[cord.x][cord.y] = x;
  45. return solve(test);
  46. }
  47.  
  48. private static boolean isFullFilled(char[][] sudoku) {
  49. for (int i = 0; i < 9; i++)
  50. for (int j = 0; j < 9; j++)
  51. if (sudoku[i][j] == '.')
  52. return false;
  53. return true;
  54. }
  55.  
  56. private static List<Character> getPossibleNr(char[][] sudoku, int x, int y) {
  57. ArrayList<Character> characters = new ArrayList<>();
  58.  
  59. if (isValid(sudoku, '1', x, y))
  60. characters.add('1');
  61. if (isValid(sudoku, '2', x, y))
  62. characters.add('2');
  63. if (isValid(sudoku, '3', x, y))
  64. characters.add('3');
  65. if (isValid(sudoku, '4', x, y))
  66. characters.add('4');
  67. if (isValid(sudoku, '5', x, y))
  68. characters.add('5');
  69. if (isValid(sudoku, '6', x, y))
  70. characters.add('6');
  71. if (isValid(sudoku, '7', x, y))
  72. characters.add('7');
  73. if (isValid(sudoku, '8', x, y))
  74. characters.add('8');
  75. if (isValid(sudoku, '9', x, y))
  76. characters.add('9');
  77.  
  78. return characters;
  79. }
  80.  
  81. private static char[][] copy(char[][] x) {
  82. char[][] tab = new char[x.length][x[0].length];
  83. for (int i = 0; i < x.length; i++)
  84. System.arraycopy(x[i], 0, tab[i], 0, x[i].length);
  85. return tab;
  86. }
  87.  
  88.  
  89. static boolean isValid(char[][] matrix, char value, int actualX, int actualY) {
  90. int howManyInSquare = howManyUnfilledInSquare(matrix, actualX, actualY, value);
  91. int howManyInColumn = howManyUnfilledInColumn(matrix, actualY, value);
  92. int howManyInRow = howManyUnfilledInRow(matrix, actualX, value);
  93.  
  94. return howManyInColumn + howManyInRow + howManyInSquare == 0;
  95. }
  96.  
  97. static Optional<Cord> findEasiestToFulfill(char[][] matrix) {
  98. Optional<Cord> cord = Optional.empty();
  99. int minUnknown = 21;
  100.  
  101. for (int x = 0; x < matrix.length; x++)
  102. for (int y = 0; y < matrix[x].length; y++) {
  103. if (matrix[x][y] == '.') {
  104.  
  105. int howManyInSquare = howManyUnfilledInSquare(matrix, x, y, '.');
  106. int howManyInColumn = howManyUnfilledInColumn(matrix, y, '.');
  107. int howManyInRow = howManyUnfilledInRow(matrix, x, '.');
  108.  
  109. int sum = howManyInColumn + howManyInRow + howManyInSquare - 3;
  110. if (minUnknown > sum) {
  111. minUnknown = sum;
  112. cord = Optional.of(new Cord(x, y));
  113. }
  114. }
  115. }
  116.  
  117. return cord;
  118. }
  119.  
  120. private static int howManyUnfilledInRow(char[][] matrix, int currentX, char value) {
  121. int unknown = 0;
  122. for (int y = 0; y < 9; y++)
  123. if (matrix[currentX][y] == value)
  124. unknown++;
  125.  
  126. return unknown;
  127. }
  128.  
  129. private static int howManyUnfilledInColumn(char[][] matrix, int currentY, char value) {
  130. int unknown = 0;
  131. for (int x = 0; x < 9; x++)
  132. if (matrix[x][currentY] == value)
  133. unknown++;
  134.  
  135. return unknown;
  136. }
  137.  
  138. private static int howManyUnfilledInSquare(char[][] matrix, int currentX, int currentY, char value) {
  139. if (currentX <= 2 && currentX >= 0 && currentY <= 2 && currentY >= 0)
  140. return count(matrix, 0, 0, value);
  141.  
  142. if (currentX <= 5 && currentX >= 3 && currentY <= 2 && currentY >= 0)
  143. return count(matrix, 3, 0, value);
  144.  
  145. if (currentX <= 8 && currentX >= 6 && currentY <= 2 && currentY >= 0)
  146. return count(matrix, 6, 0, value);
  147.  
  148.  
  149. if (currentX <= 2 && currentX >= 0 && currentY <= 5 && currentY >= 3)
  150. return count(matrix, 0, 3, value);
  151.  
  152. if (currentX <= 5 && currentX >= 3 && currentY <= 5 && currentY >= 3)
  153. return count(matrix, 3, 3, value);
  154.  
  155. if (currentX <= 8 && currentX >= 6 && currentY <= 5 && currentY >= 3)
  156. return count(matrix, 6, 3, value);
  157.  
  158.  
  159. if (currentX <= 2 && currentX >= 0 && currentY <= 8 && currentY >= 6)
  160. return count(matrix, 0, 6, value);
  161.  
  162. if (currentX <= 5 && currentX >= 3 && currentY <= 8 && currentY >= 6)
  163. return count(matrix, 3, 6, value);
  164.  
  165. if (currentX <= 8 && currentX >= 6 && currentY <= 8 && currentY >= 6)
  166. return count(matrix, 6, 6, value);
  167.  
  168. return 0;
  169. }
  170.  
  171. private static int count(char[][] matrix, int x, int y, char value) {
  172. int unknown = 0;
  173. for (int i = 0; i < 3; i++)
  174. for (int j = 0; j < 3; j++)
  175. if (matrix[(x + i)][y + j] == value)
  176. unknown++;
  177.  
  178. return unknown;
  179. }
  180.  
  181. private static class Cord {
  182.  
  183. int x;
  184. int y;
  185.  
  186. Cord(int x, int y) {
  187. this.x = x;
  188. this.y = y;
  189. }
  190. }
  191.  
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement