Guest User

Untitled

a guest
Jan 20th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. /* Jeroen van Hoof
  2. * 13-15 September 2011
  3. * Cellulitis
  4. */
  5.  
  6. import java.util.Scanner;
  7.  
  8. class Sudoku {
  9. int SIZE = 9; // size of the grid
  10. int DMAX = 9; // maximal digit to be filled in
  11. int BOXSIZE = 3; // size of the boxes (subgrids that should contain all digits)
  12. int[][] grid; // the puzzle grid; 0 represents empty
  13. int solutionnr = 0; //solution counter
  14.  
  15. // coordinates of next empty square
  16. int rempty;
  17. int cempty;
  18.  
  19. // ----------------- conflict calculation --------------------
  20.  
  21. // is there a conflict when we fill in d at position r,c?
  22. //@pre: 1<=n && n<=DMAX && grid[r][c]==0
  23. //@post: ...
  24.  
  25. boolean givesConflict(int r, int c, int n) { // r = row, c = column, n = number
  26. if (rowConflict(r, n) || colConflict(c, n) || boxConflict(r, c, n)) {
  27. return true;
  28. } else {
  29. return false;
  30. }
  31. }
  32.  
  33. //Is there a row conflict for value d in row r?
  34. //@ pre 1 <- d <= g && 1 <= r <= g
  35. //@ post return true iff grid grid[i][x] == d for some 1 <= for some 1 <= r <= g
  36. boolean rowConflict(int r, int n) {
  37. for (int i = 0; i < SIZE; i++){
  38. if (grid[i][r] == n){
  39. return true;
  40. }
  41. }
  42. return false;
  43. }
  44.  
  45. boolean colConflict(int c, int n) {
  46. for (int i = 0; i < SIZE; i++){
  47. if (grid[c][i] == n){
  48. return true;
  49. }
  50. }
  51. return false;
  52. }
  53.  
  54. boolean boxConflict(int r, int c, int n) {
  55. for (int i = 0; i < BOXSIZE; i++){
  56. for (int j = 0; j < BOXSIZE; j++){
  57. if (grid[(int) Math.floor(r/3) + i][(int) Math.floor(c/3) + j] == n) {
  58. return true;
  59. }
  60. }
  61. }
  62. return false;
  63. }
  64.  
  65. // --------- solving ----------
  66.  
  67. // finds the next empty square (in "reading order")
  68. // writes the coordinates in rempty and cempty
  69. // returns false if there is no empty square in the current grid
  70. boolean findEmptySquare() {
  71. for (int i = 0; i < SIZE; i++){
  72. for (int j = 0; j < SIZE; j++){
  73. if (grid[j][i] == 0) {
  74. cempty = j;
  75. rempty = i;
  76. return true;
  77. }
  78. }
  79. }
  80. return false;
  81. }
  82.  
  83.  
  84. // prints all solutions that are extensions of current grid
  85. // leaves grid in original state
  86. boolean solve() {
  87. if (!findEmptySquare()){
  88. print();
  89. return false;
  90. }
  91. else {
  92. for (int n = 1; n < 9; n++){
  93. if (!givesConflict(rempty, cempty, n)){
  94. grid[rempty][cempty] = n;
  95. if (solve()){
  96. return true;
  97. }
  98. }
  99. }
  100. grid[rempty][cempty] = 0; // reset on backtrack
  101. return false;
  102. }
  103. }
  104.  
  105. // ------------------------- misc -------------------------
  106.  
  107. // print the grid, 0s are printed as spaces
  108. void print() {
  109.  
  110. String line = "";
  111. String line1 = "-------------------------";
  112. String line2 = "-------------------------";
  113.  
  114. System.out.println(line2);
  115. for (int i = 0; i < SIZE; i++){
  116. if ( i%3 == 0 && i != 0){
  117. System.out.println(line1);
  118. }
  119. for (int j = 0; j < SIZE; j++){
  120. if ( j%3 == 0){
  121. line += "| ";
  122. }
  123. if (grid[i][j] == 0){
  124. line += ". ";
  125. }else{
  126. line += grid[i][j] + " ";
  127. }
  128. }
  129. System.out.println(line + "|");
  130. line = "";
  131. }
  132. System.out.println(line2);
  133. }
  134.  
  135. // --------------- where it all starts --------------------
  136.  
  137. void solveIt() {
  138. // I got some help here from Floris Schippers and Hein van Beers
  139. Scanner scanner = new Scanner(System.in);
  140.  
  141. grid = new int[9][9];
  142. for(int i = 0; i < SIZE; i++){
  143. for(int j = 0; j < SIZE; j++){
  144. boolean StopLoop = false;
  145. while(!scanner.hasNextInt() && !StopLoop){
  146. if(".".equals(scanner.next())){
  147. StopLoop = true;
  148. }
  149. }
  150. if(StopLoop){
  151. grid[i][j] = 0;
  152. }else
  153. grid[i][j] = scanner.nextInt();
  154. }
  155. }
  156.  
  157. if(solutionnr == 1){System.out.println("Found " + solutionnr + " solution");
  158. }else{System.out.println("Found " + solutionnr + " solutions");
  159. }
  160. }
  161.  
  162.  
  163.  
  164. public static void main(String[] args) {
  165. new Sudoku().solveIt();
  166. new Sudoku().print();
  167. }
  168. }
Add Comment
Please, Sign In to add comment