Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.75 KB | None | 0 0
  1. package org.excuseme.sudokusolver;
  2.  
  3. import java.time.Instant;
  4. import java.time.temporal.ChronoUnit;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import java.util.Objects;
  8. import java.util.stream.Collectors;
  9. import java.util.stream.IntStream;
  10. import java.util.stream.Stream;
  11.  
  12. public class DepthFirstSudokuPuzzleSolver {
  13. public static final String[][] PUZZLE_A = new String[][]
  14. {
  15. {"", "", "", "2", "6", "", "7", "", "1"},
  16. {"6", "8", "", "", "7", "", "", "9", ""},
  17. {"1", "9", "", "", "", "4", "5", "", ""},
  18. {"8", "2", "", "1", "", "", "", "4", ""},
  19. {"", "", "4", "6", "", "2", "9", "", ""},
  20. {"", "5", "", "", "", "3", "", "2", "8"},
  21. {"", "", "9", "3", "", "", "", "7", "4"},
  22. {"", "4", "", "", "5", "", "", "3", "6"},
  23. {"7", "", "3", "", "1", "8", "", "", ""},
  24. };
  25. public static final String[][] PUZZLE_B = new String[][]
  26. {
  27. {"","K","","", "N","O","","", "","","A","J", "","","","E"},
  28. {"","","","", "","","","", "","","","", "","","",""},
  29. {"F","L","","", "","I","A","K", "","","","O", "B","J","P","H"},
  30. {"H","","","", "F","B","","", "C","","P","", "I","N","L","D"},
  31.  
  32. {"","","","", "","","","", "","","","", "","","",""},
  33. {"","D","","", "J","F","","E", "H","P","K","A", "L","I","C",""},
  34. {"","J","C","", "K","","","D", "I","B","M","L", "E","","A",""},
  35. {"I","B","M","", "","","","N", "E","","","", "","","J","F"},
  36.  
  37. {"","O","B","K", "I","","P","A", "L","","H","D", "","","E","M"},
  38. {"","C","","", "","H","","", "K","","N","F", "A","","",""},
  39. {"","","","", "","","","", "","","","", "","","",""},
  40. {"J","","","", "","","","", "","","","", "","","","I"},
  41.  
  42. {"","","","", "B","","","", "","L","","", "J","","",""},
  43. {"","I","","", "A","N","H","J", "M","D","E","C", "F","","","O"},
  44. {"N","","","", "D","","","", "A","","","", "","","",""},
  45. {"","","D","", "L","","","M", "J","","I","", "P","","B","K"}
  46. };
  47. public static void main(String[] args) {
  48. final PrettyPrinter prettyPrinter = new PrettyPrinter(System.out);
  49. Instant starts = Instant.now();
  50. int length = 16;
  51.  
  52. String[][] solution = new DepthFirstSudokuPuzzleSolver("123456789".toUpperCase().toCharArray()).solve(PUZZLE_A);
  53. Instant ends = Instant.now();
  54.  
  55. prettyPrinter.print(solution);
  56. System.out.println("Executed in : "+ ChronoUnit.MILLIS.between(starts, ends) + " ms");
  57.  
  58. }
  59. private final char[] characters;
  60.  
  61. public DepthFirstSudokuPuzzleSolver(char[] characters) {
  62. this.characters = characters;
  63. }
  64.  
  65. private String[][] solve(String[][] puzzle) {
  66. return solveRecursive(puzzle, 0, 0);
  67. }
  68.  
  69. private String[][] solveRecursive(String[][] puzzle, int startRow, int startColumn) {
  70. for (int row = startRow; row < puzzle.length; row++) {
  71. for (int column = startColumn; column < puzzle.length; column++) {
  72. final String cellValue = puzzle[row][column];
  73. if (Objects.equals(cellValue, "")) {
  74. final List<String> possibleValues = getPossibleValues(puzzle, row, column);
  75. if (!possibleValues.isEmpty()) {
  76. for (String possibleValue : possibleValues) {
  77. String[][] modifiedPuzzle = cloneArray(puzzle);
  78. modifiedPuzzle[row][column] = possibleValue;
  79. int newColumn = column + 1;
  80. int newRow = row;
  81.  
  82. if (newColumn == puzzle.length) {
  83. newColumn = 0;
  84. newRow++;
  85. }
  86.  
  87. final String[][] solution = solveRecursive(modifiedPuzzle, newRow, newColumn);
  88. if (solution != null) {
  89. return solution;
  90. }
  91. }
  92. }
  93. return null;
  94. }
  95. }
  96. startColumn = 0;
  97. }
  98. return puzzle;
  99. }
  100.  
  101.  
  102. private List<String> getRowValues(String[][] puzzle, int row) {
  103. final List<String> integers = new ArrayList<>();
  104. for (String value : puzzle[row]) {
  105. integers.add(value);
  106. }
  107. return integers;
  108. }
  109.  
  110. private List<String> getColumnValues(String[][] puzzle, int column) {
  111. final List<String> columnValues = new ArrayList<>();
  112. for (int y = 0; y < puzzle.length; y++) {
  113. columnValues.add(puzzle[y][column]);
  114. }
  115. return columnValues;
  116. }
  117.  
  118. private List<String> getBlockValues(String[][] puzzle, int row, int column) {
  119. final List<String> columnValues = new ArrayList<>();
  120. final int sr = (int) Math.sqrt(puzzle.length);
  121. int blockStartColumn = column - (column % sr);
  122. int blockStartRow = row - (row % sr);
  123. for (int indexRow = blockStartRow; indexRow < blockStartRow + sr; indexRow++) {
  124. for (int indexColumn = blockStartColumn; indexColumn < blockStartColumn + sr; indexColumn++) {
  125. columnValues.add(puzzle[indexRow][indexColumn]);
  126. }
  127. }
  128. return columnValues;
  129. }
  130.  
  131. private List<String> getPossibleValues(String[][] puzzle, int row, int column) {
  132. Stream<String> cStream = IntStream.range(0, characters.length).mapToObj(i ->String.valueOf(characters[i]));
  133. final List<String> characters = new ArrayList<String>(cStream.collect(Collectors.toList()));
  134. final List<String> rowValues = getRowValues(puzzle, row);
  135. characters.removeAll(rowValues);
  136. final List<String> columnValues = getColumnValues(puzzle, column);
  137. characters.removeAll(columnValues);
  138. final List<String> blockValues = getBlockValues(puzzle, row, column);
  139. characters.removeAll(blockValues);
  140. return characters;
  141. }
  142.  
  143. private static String[][] cloneArray(String[][] src) {
  144. int length = src.length;
  145. String[][] target = new String[length][src[0].length];
  146. for (int i = 0; i < length; i++) {
  147. System.arraycopy(src[i], 0, target[i], 0, src[i].length);
  148. }
  149. return target;
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement