|
| 1 | +package com.thealgorithms.backtracking; |
| 2 | + |
| 3 | +/** |
| 4 | + * Program description - Solves a given 9x9 Sudoku puzzle using backtracking. |
| 5 | + * <a href="https://en.wikipedia.org/wiki/Sudoku">Wikipedia</a> |
| 6 | + * |
| 7 | + * @author <a href="https://github.com/codewithRakshita">Rakshita</a> |
| 8 | + */ |
| 9 | +public class SudokuSolver { |
| 10 | + |
| 11 | + private static final int SIZE = 9; // size of the Sudoku grid |
| 12 | + |
| 13 | + /** |
| 14 | + * Solves the Sudoku puzzle by filling empty cells. |
| 15 | + * |
| 16 | + * @param board The 9x9 Sudoku puzzle grid |
| 17 | + * @return true if the Sudoku puzzle is solvable, false otherwise |
| 18 | + */ |
| 19 | + public boolean solveSudoku(int[][] board) { |
| 20 | + for (int row = 0; row < SIZE; row++) { |
| 21 | + for (int col = 0; col < SIZE; col++) { |
| 22 | + |
| 23 | + // Check for an empty cell |
| 24 | + if (board[row][col] == 0) { |
| 25 | + |
| 26 | + // Try numbers 1 to 9 |
| 27 | + for (int num = 1; num <= SIZE; num++) { |
| 28 | + if (isSafe(board, row, col, num)) { |
| 29 | + board[row][col] = num; |
| 30 | + |
| 31 | + // Recursively try filling the rest |
| 32 | + if (solveSudoku(board)) { |
| 33 | + return true; |
| 34 | + } |
| 35 | + |
| 36 | + // Backtrack |
| 37 | + board[row][col] = 0; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + // No valid number found — backtrack |
| 42 | + return false; |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + return true; // solved |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Checks whether placing a number at a specific position is valid. |
| 52 | + */ |
| 53 | + private boolean isSafe(int[][] board, int row, int col, int num) { |
| 54 | + // Check row |
| 55 | + for (int x = 0; x < SIZE; x++) { |
| 56 | + if (board[row][x] == num) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // Check column |
| 62 | + for (int x = 0; x < SIZE; x++) { |
| 63 | + if (board[x][col] == num) { |
| 64 | + return false; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // Check 3x3 sub-grid |
| 69 | + int startRow = row - row % 3; |
| 70 | + int startCol = col - col % 3; |
| 71 | + |
| 72 | + for (int i = 0; i < 3; i++) { |
| 73 | + for (int j = 0; j < 3; j++) { |
| 74 | + if (board[i + startRow][j + startCol] == num) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return true; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Prints the Sudoku grid. |
| 85 | + */ |
| 86 | + public void printBoard(int[][] board) { |
| 87 | + for (int r = 0; r < SIZE; r++) { |
| 88 | + for (int d = 0; d < SIZE; d++) { |
| 89 | + System.out.print(board[r][d] + " "); |
| 90 | + } |
| 91 | + System.out.println(); |
| 92 | + } |
| 93 | + System.out.println(); |
| 94 | + } |
| 95 | + |
| 96 | + // Example usage (for testing) |
| 97 | + public static void main(String[] args) { |
| 98 | + int[][] board = { |
| 99 | + {5, 3, 0, 0, 7, 0, 0, 0, 0}, |
| 100 | + {6, 0, 0, 1, 9, 5, 0, 0, 0}, |
| 101 | + {0, 9, 8, 0, 0, 0, 0, 6, 0}, |
| 102 | + {8, 0, 0, 0, 6, 0, 0, 0, 3}, |
| 103 | + {4, 0, 0, 8, 0, 3, 0, 0, 1}, |
| 104 | + {7, 0, 0, 0, 2, 0, 0, 0, 6}, |
| 105 | + {0, 6, 0, 0, 0, 0, 2, 8, 0}, |
| 106 | + {0, 0, 0, 4, 1, 9, 0, 0, 5}, |
| 107 | + {0, 0, 0, 0, 8, 0, 0, 7, 9} |
| 108 | + }; |
| 109 | + |
| 110 | + SudokuSolver solver = new SudokuSolver(); |
| 111 | + if (solver.solveSudoku(board)) { |
| 112 | + System.out.println("Solved Sudoku:"); |
| 113 | + solver.printBoard(board); |
| 114 | + } else { |
| 115 | + System.out.println("No solution exists."); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments