|
| 1 | +public class Sudoku { |
| 2 | + public static void main(String[] args) { |
| 3 | + int[][] board = new int[][]{ |
| 4 | + {0, 0, 6, 5, 0, 8, 4, 0, 0}, |
| 5 | + {5, 2, 0, 0, 0, 0, 0, 0, 0}, |
| 6 | + {0, 8, 7, 0, 0, 0, 0, 3, 1}, |
| 7 | + {0, 0, 3, 0, 1, 0, 0, 8, 0}, |
| 8 | + {9, 0, 0, 8, 6, 3, 0, 0, 5}, |
| 9 | + {0, 5, 0, 0, 9, 0, 6, 0, 0}, |
| 10 | + {1, 3, 0, 0, 0, 0, 2, 5, 0}, |
| 11 | + {0, 0, 0, 0, 0, 0, 0, 7, 4}, |
| 12 | + {0, 0, 5, 2, 0, 6, 3, 0, 0} |
| 13 | + }; |
| 14 | + |
| 15 | + if (solve(board)) { |
| 16 | + display(board); |
| 17 | + } else { |
| 18 | + System.out.println("Cannot solve"); |
| 19 | + } |
| 20 | + |
| 21 | + } |
| 22 | + |
| 23 | + static boolean solve(int[][] board) { |
| 24 | + int n = board.length; |
| 25 | + int row = -1; |
| 26 | + int col = -1; |
| 27 | + |
| 28 | + boolean emptyLeft = true; |
| 29 | + |
| 30 | + // this is how we are replacing the r,c from arguments |
| 31 | + for (int i = 0; i < n; i++) { |
| 32 | + for (int j = 0; j < n; j++) { |
| 33 | + if (board[i][j] == 0) { |
| 34 | + row = i; |
| 35 | + col = j; |
| 36 | + emptyLeft = false; |
| 37 | + break; |
| 38 | + } |
| 39 | + } |
| 40 | + // if you found some empty element in row, then break |
| 41 | + if (emptyLeft == false) { |
| 42 | + break; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + if (emptyLeft == true) { |
| 47 | + return true; |
| 48 | + // soduko is solved |
| 49 | + } |
| 50 | + |
| 51 | + // backtrack |
| 52 | + for (int number = 1; number <= 9; number++) { |
| 53 | + if (isSafe(board, row, col, number)) { |
| 54 | + board[row][col] = number; |
| 55 | + if (solve(board)) { |
| 56 | + // found the answer |
| 57 | + return true; |
| 58 | + } else { |
| 59 | + // backtrack |
| 60 | + board[row][col] = 0; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + private static void display(int[][] board) { |
| 68 | + for(int[] row : board) { |
| 69 | + for(int num : row) { |
| 70 | + System.out.print(num + " "); |
| 71 | + } |
| 72 | + System.out.println(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + static boolean isSafe(int[][] board, int row, int col, int num) { |
| 78 | + // check the row |
| 79 | + for (int i = 0; i < board.length; i++) { |
| 80 | + // check if the number is in the row |
| 81 | + if (board[row][i] == num) { |
| 82 | + return false; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // check the col |
| 87 | + for (int[] nums : board) { |
| 88 | + // check if the number is in the col |
| 89 | + if (nums[col] == num) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + int sqrt = (int)(Math.sqrt(board.length)); |
| 95 | + int rowStart = row - row % sqrt; |
| 96 | + int colStart = col - col % sqrt; |
| 97 | + |
| 98 | + for (int r = rowStart; r < rowStart + sqrt; r++) { |
| 99 | + for (int c = colStart; c < colStart + sqrt; c++) { |
| 100 | + if (board[r][c] == num) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + return true; |
| 106 | + } |
| 107 | +} |
0 commit comments