Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.50 KB | None | 0 0
  1. package csd.uoc.gr.A14;
  2. import java.util.Random;
  3. import java.util.Scanner;
  4. import java.util.stream.IntStream;
  5. class Sudoku {
  6. private static final int BOARD_SIZE = 9;
  7. private static final int SUBSECTION_SIZE = 3;
  8. private static final int BOARD_START_INDEX = 0;
  9.  
  10. private static final int NO_VALUE = 0;
  11. private static final int MIN_VALUE = 1;
  12. private static final int MAX_VALUE = 9;
  13.  
  14. private static int[][] board = {
  15. {8, 0, 0, 0, 0, 0, 0, 0, 0},
  16. {0, 0, 3, 6, 0, 0, 0, 0, 0},
  17. {0, 7, 0, 0, 9, 0, 2, 0, 0},
  18. {0, 5, 0, 0, 0, 7, 0, 0, 0},
  19. {0, 0, 0, 0, 4, 5, 7, 0, 0},
  20. {0, 0, 0, 1, 0, 0, 0, 3, 0},
  21. {0, 0, 1, 0, 0, 0, 0, 6, 8},
  22. {0, 0, 8, 5, 0, 0, 0, 1, 0},
  23. {0, 9, 0, 0, 0, 0, 4, 0, 6}
  24. };
  25.  
  26. public static void main (String[]args){
  27. Sudoku solver = new Sudoku();
  28. solver.solve(board);
  29. solver.printBoard();
  30. NewSudoku();
  31. }
  32.  
  33.  
  34. private void printBoard () {
  35. for (int row = BOARD_START_INDEX; row < BOARD_SIZE; row++) {
  36. for (int column = BOARD_START_INDEX; column < BOARD_SIZE; column++) {
  37. System.out.print(board[row][column] + " ");
  38. }
  39. System.out.println();
  40. }
  41. }
  42. private static boolean solve ( int[][] board){
  43. for (int row = BOARD_START_INDEX; row < BOARD_SIZE; row++) {
  44. for (int column = BOARD_START_INDEX; column < BOARD_SIZE; column++) {
  45. if (board[row][column] == NO_VALUE) {
  46. for (int k = MIN_VALUE; k <= MAX_VALUE; k++) {
  47. board[row][column] = k;
  48. if (isValid(board, row, column) && solve(board)) {
  49. return true;
  50. }
  51. board[row][column] = NO_VALUE;
  52. }
  53. return false;
  54. }
  55. }
  56. }
  57. return true;
  58. }
  59.  
  60. private static boolean isValid ( int[][] board, int row, int column){
  61. return rowConstraint(board, row) &&
  62. columnConstraint(board, column) &&
  63. subsectionConstraint(board, row, column);
  64. }
  65.  
  66. private static boolean subsectionConstraint ( int[][] board, int row, int column){
  67. boolean[] constraint = new boolean[BOARD_SIZE];
  68. int subsectionRowStart = (row / SUBSECTION_SIZE) * SUBSECTION_SIZE;
  69. int subsectionRowEnd = subsectionRowStart + SUBSECTION_SIZE;
  70.  
  71. int subsectionColumnStart = (column / SUBSECTION_SIZE) * SUBSECTION_SIZE;
  72. int subsectionColumnEnd = subsectionColumnStart + SUBSECTION_SIZE;
  73.  
  74. for (int r = subsectionRowStart; r < subsectionRowEnd; r++) {
  75. for (int c = subsectionColumnStart; c < subsectionColumnEnd; c++) {
  76. if (!checkConstraint(board, r, constraint, c)) return false;
  77. }
  78. }
  79. return true;
  80. }
  81.  
  82. private static boolean columnConstraint ( int[][] board, int column){
  83. boolean[] constraint = new boolean[BOARD_SIZE];
  84. return IntStream.range(BOARD_START_INDEX, BOARD_SIZE)
  85. .allMatch(row -> checkConstraint(board, row, constraint, column));
  86. }
  87.  
  88. private static boolean rowConstraint ( int[][] board, int row){
  89. boolean[] constraint = new boolean[BOARD_SIZE];
  90. return IntStream.range(BOARD_START_INDEX, BOARD_SIZE)
  91. .allMatch(column -> checkConstraint(board, row, constraint, column));
  92. }
  93.  
  94. private static boolean checkConstraint ( int[][] board, int row, boolean[] constraint, int column){
  95. if (board[row][column] != NO_VALUE) {
  96. if (!constraint[board[row][column] - 1]) {
  97. constraint[board[row][column] - 1] = true;
  98. } else {
  99. return false;
  100. }
  101. }
  102. return true;
  103. }
  104. private static boolean isValidBoard(int[][] brd){
  105. for (int i=0;i< BOARD_SIZE;i++) {
  106. for (int j = 0; j < BOARD_SIZE; j++) {
  107. if (brd[i][j] != 0) {
  108. for (int k = 0; k < BOARD_SIZE; k++) {
  109. if (j!=k &&(brd[i][j]==brd[i][k])){
  110. System.out.println("Found duplicates in rows.");
  111. return true;
  112. }
  113. }
  114. }
  115. if (brd [j][i]!=0){
  116. for (int k=0;k<BOARD_SIZE;k++){
  117. if (j!=k &&(brd[j][i]==brd[k][i])){
  118. System.out.println("Found duplicates in columns.");
  119. return true;
  120. }
  121. }
  122. }
  123. }
  124. }
  125. for (int subcounter =0;subcounter<9;subcounter=subcounter+3) {
  126. for (int i = subcounter; i < SUBSECTION_SIZE+subcounter; i++) {
  127. for (int j = subcounter; j < SUBSECTION_SIZE+subcounter; j++) {
  128. int key = brd[i][j];
  129. for (int k = subcounter; k < SUBSECTION_SIZE+subcounter; k++) {
  130. for (int l = subcounter; l < SUBSECTION_SIZE+subcounter; l++) {
  131. if (((i != k) || (j != l)) && (key == brd[k][l])&&(key!=0)) {
  132. System.out.println("Found duplicates in a subsection.");
  133. return true;
  134. }
  135. }
  136. }
  137. }
  138. }
  139. }
  140. return false ;
  141. }
  142. private static boolean isSolvableBoard(int [][]board){
  143. if (isValidBoard(board)|| !solve(board)){
  144. System.out.println("Can't solve the board.");
  145. return false;
  146. }
  147. return true;
  148. }
  149. public static int [][] BoardGenerator(int X){
  150. int [][] Xboard =new int [BOARD_SIZE][BOARD_SIZE];
  151. Random randomGenerator = new Random();
  152. int randomInt = randomGenerator.nextInt(MAX_VALUE-MIN_VALUE+1) + MIN_VALUE;
  153. for (int numRandoms=0;numRandoms<81-X;numRandoms++){//how many random numbers
  154. int i = randomGenerator.nextInt(MAX_VALUE-MIN_VALUE+1) + MIN_VALUE;
  155. int j = randomGenerator.nextInt(MAX_VALUE-MIN_VALUE+1) + MIN_VALUE;
  156. while (Xboard[i][j]!=0) {
  157. i = randomGenerator.nextInt(MAX_VALUE-MIN_VALUE+1) + MIN_VALUE;;
  158. j = randomGenerator.nextInt(MAX_VALUE-MIN_VALUE+1) + MIN_VALUE;;
  159. }
  160. Xboard[i][j]=randomInt;
  161. randomInt = randomGenerator.nextInt(MAX_VALUE-MIN_VALUE+1) + MIN_VALUE;
  162. }
  163. return Xboard;
  164. }
  165. public static void NewSudoku (){
  166. System.out.println("Choose the number of the sudoku you want to create:");
  167. Scanner in = new Scanner(System.in);
  168. int N = in.nextInt();//number of Sudoku
  169. if (N<0){
  170. System.out.println("Wrong input!");
  171. return;
  172. }
  173. int amountSudoku =1;
  174. int unsolvable =0;
  175. int valid =0;
  176. int invalid=0;
  177. int [][] newBoard = new int [BOARD_SIZE][BOARD_SIZE];
  178. int [][] newBoardcopy = new int [BOARD_SIZE][BOARD_SIZE];
  179.  
  180. System.out.println("Choose the number of the empty cells");
  181. in = new Scanner(System.in);
  182. int X = in.nextInt();
  183. if (X<0||X>81){
  184. System.out.println("Wrong Input of empty cells !");
  185. return;
  186. }
  187. int i =1;
  188. long start= System.currentTimeMillis();
  189. while(i<=N) {//counting until we find N solved boards
  190. newBoard = BoardGenerator(X) ;
  191. for (int j=0;j<BOARD_SIZE;j++){
  192. for (int k=0;k<BOARD_SIZE;k++){
  193. System.out.println("Board #"+amountSudoku);
  194. newBoardcopy[j][k]= newBoard[j][k];
  195. }
  196. }
  197. if (isValidBoard(newBoard)){
  198. invalid++;
  199. }else{
  200. for (int j=0;j<BOARD_SIZE;j++){
  201. for (int k=0;k<BOARD_SIZE;k++){
  202. System.out.println("Solution of the Board #"+amountSudoku);
  203. System.out.println(newBoardcopy[j][k]);
  204. }
  205. }
  206. valid++;
  207. }
  208. if (isSolvableBoard(newBoardcopy)){
  209. for (int j=0;j<BOARD_SIZE;j++){
  210. for (int k=0;k<BOARD_SIZE;k++){
  211. System.out.println(newBoard[j][k]);
  212. }
  213. }
  214. i++;
  215. }else{
  216. unsolvable++;
  217. }
  218. amountSudoku ++;
  219. }
  220. long end = System.currentTimeMillis();
  221. float sec = (end -start) / 1000F;
  222. System.out.println("Empty cells per board :"+X);
  223. System.out.println("Valid boards created :"+valid);
  224. System.out.println("Invalid boards created :"+invalid);
  225. System.out.println("Unsolvable boards created :"+valid);
  226. System.out.println("Elapsed time in seconds : :"+sec);
  227.  
  228. }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement