├── README.md └── SudokuSolver.java /README.md: -------------------------------------------------------------------------------- 1 | # Prodigy_SD_04 2 | PRODIGY_SD_04 3 | 4 | ```markdown 5 | # Sudoku Solver 6 | 7 | This project is a Sudoku solver program that automatically solves Sudoku puzzles. It takes an input grid representing an unsolved Sudoku puzzle and uses backtracking or other suitable techniques to fill in the missing numbers. Once solved, the program displays the completed Sudoku grid. 8 | 9 | ## Features 10 | 11 | - **Automatic solving:** The program automatically solves Sudoku puzzles. 12 | - **Backtracking:** It uses backtracking or other suitable techniques to explore possible solutions and find the correct arrangement of numbers. 13 | - **User feedback:** The program informs the user whether the Sudoku puzzle was solved successfully or if no solution exists. 14 | 15 | ## How to Use 16 | 17 | 1. Clone this repository to your local machine: 18 | 19 | ```sh 20 | git clone https://github.com/oraclebrain/sudoku-solver.git 21 | ``` 22 | 23 | 2. Navigate to the project directory: 24 | 25 | ```sh 26 | cd sudoku-solver 27 | ``` 28 | 29 | 3. Compile the Java program: 30 | 31 | ```sh 32 | javac SudokuSolver.java 33 | ``` 34 | 35 | 4. Run the program: 36 | 37 | ```sh 38 | java SudokuSolver 39 | ``` 40 | 41 | 5. Follow the on-screen instructions to see the solved Sudoku puzzle. 42 | 43 | ## Example 44 | 45 | ``` 46 | TASK 02 47 | The program is represented by Jeegnasa Makwana 48 | Sudoku puzzle solved successfully! 49 | 5 3 4 6 7 8 9 1 2 50 | 6 7 2 1 9 5 3 4 8 51 | 1 9 8 3 4 2 5 6 7 52 | 8 5 9 7 6 1 4 2 3 53 | 4 2 6 8 5 3 7 9 1 54 | 7 1 3 9 2 4 8 5 6 55 | 9 6 1 5 3 7 2 8 4 56 | 2 8 7 4 1 9 6 3 5 57 | 3 4 5 2 8 6 1 7 9 58 | ``` 59 | 60 | ## Code Overview 61 | 62 | ```java 63 | public class SudokuSolver { 64 | public static void main(String[] args) { 65 | int[][] board = { 66 | {5, 3, 0, 0, 7, 0, 0, 0, 0}, 67 | {6, 0, 0, 1, 9, 5, 0, 0, 0}, 68 | {0, 9, 8, 0, 0, 0, 0, 6, 0}, 69 | {8, 0, 0, 0, 6, 0, 0, 0, 3}, 70 | {4, 0, 0, 8, 0, 3, 0, 0, 1}, 71 | {7, 0, 0, 0, 2, 0, 0, 0, 6}, 72 | {0, 6, 0, 0, 0, 0, 2, 8, 0}, 73 | {0, 0, 0, 4, 1, 9, 0, 0, 5}, 74 | {0, 0, 0, 0, 8, 0, 0, 7, 9} 75 | }; 76 | 77 | if (solveSudoku(board)) { 78 | System.out.println("TASK 02"); 79 | System.out.println("The program is represented by Jeegnasa Makwana"); 80 | System.out.println("Sudoku puzzle solved successfully!"); 81 | printBoard(board); 82 | } else { 83 | System.out.println("No solution exists for the given Sudoku puzzle."); 84 | } 85 | } 86 | 87 | // Additional methods are omitted for brevity 88 | } 89 | ``` 90 | 91 | - The `main` method initializes the Sudoku puzzle grid, attempts to solve it using the `solveSudoku` method, and displays the solved puzzle if successful. 92 | - The `solveSudoku` method employs backtracking to explore possible solutions and find the correct arrangement of numbers. 93 | - Additional methods such as `findEmptyCell`, `isValidMove`, and `printBoard` are used to support the solving process. 94 | 95 | ## Contributing 96 | 97 | Contributions are welcome! Please open an issue or submit a pull request for any enhancements or bug fixes. 98 | 99 | ## License 100 | 101 | This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. 102 | ``` 103 | 104 | Feel free to customize this `README.md` file further to suit your specific project needs or preferences. 105 | ``` -------------------------------------------------------------------------------- /SudokuSolver.java: -------------------------------------------------------------------------------- 1 | ```java 2 | /** 3 | * The SudokuSolver class solves a given Sudoku puzzle using backtracking. 4 | * It represents a Sudoku puzzle as a 9x9 grid of integers, where empty cells are represented by zeros. 5 | * The class provides methods to solve the puzzle and print the solved board. 6 | * 7 | * Author: Aashis Jha 8 | */ 9 | public class SudokuSolver { 10 | /** 11 | * The main method initializes the Sudoku board with the puzzle to solve. 12 | * It then attempts to solve the puzzle and prints the solved board if a solution exists. 13 | */ 14 | public static void main(String[] args) { 15 | // Initialize the Sudoku puzzle board 16 | int[][] board = { 17 | {5, 3, 0, 0, 7, 0, 0, 0, 0}, 18 | {6, 0, 0, 1, 9, 5, 0, 0, 0}, 19 | {0, 9, 8, 0, 0, 0, 0, 6, 0}, 20 | {8, 0, 0, 0, 6, 0, 0, 0, 3}, 21 | {4, 0, 0, 8, 0, 3, 0, 0, 1}, 22 | {7, 0, 0, 0, 2, 0, 0, 0, 6}, 23 | {0, 6, 0, 0, 0, 0, 2, 8, 0}, 24 | {0, 0, 0, 4, 1, 9, 0, 0, 5}, 25 | {0, 0, 0, 0, 8, 0, 0, 7, 9} 26 | }; 27 | 28 | // Solve the Sudoku puzzle 29 | if (solveSudoku(board)) { 30 | // Print solved puzzle if solution exists 31 | System.out.println("TASK 02"); 32 | System.out.println("The program is represented by Aashis Jha"); 33 | System.out.println("Sudoku puzzle solved successfully!"); 34 | printBoard(board); 35 | } else { 36 | // Print message if no solution exists 37 | System.out.println("No solution exists for the given Sudoku puzzle."); 38 | } 39 | } 40 | 41 | /** 42 | * Recursively solves the Sudoku puzzle using backtracking. 43 | * 44 | * @param board The Sudoku puzzle board. 45 | * @return True if a solution is found, false otherwise. 46 | */ 47 | public static boolean solveSudoku(int[][] board) { 48 | int[] emptyCell = findEmptyCell(board); 49 | 50 | // Base case: if no empty cell is found, puzzle is solved 51 | if (emptyCell == null) { 52 | return true; 53 | } 54 | 55 | int row = emptyCell[0]; 56 | int col = emptyCell[1]; 57 | 58 | // Try numbers 1 to 9 in the empty cell 59 | for (int num = 1; num <= 9; num++) { 60 | if (isValidMove(board, row, col, num)) { 61 | // Place the valid number and recursively solve the puzzle 62 | board[row][col] = num; 63 | if (solveSudoku(board)) { 64 | return true; 65 | } 66 | // Undo the move if no solution is found 67 | board[row][col] = 0; 68 | } 69 | } 70 | 71 | // No valid number found for the current cell, backtrack 72 | return false; 73 | } 74 | 75 | /** 76 | * Finds the first empty cell in the Sudoku board. 77 | * 78 | * @param board The Sudoku puzzle board. 79 | * @return An array containing the row and column indices of the empty cell, or null if no empty cell is found. 80 | */ 81 | public static int[] findEmptyCell(int[][] board) { 82 | int[] cell = new int[2]; 83 | 84 | // Iterate through the board to find the first empty cell 85 | for (int row = 0; row < 9; row++) { 86 | for (int col = 0; col < 9; col++) { 87 | if (board[row][col] == 0) { 88 | cell[0] = row; 89 | cell[1] = col; 90 | return cell; 91 | } 92 | } 93 | } 94 | 95 | // Return null if no empty cell is found 96 | return null; 97 | } 98 | 99 | /** 100 | * Checks if placing a number in a given cell is a valid move. 101 | * 102 | * @param board The Sudoku puzzle board. 103 | * @param row The row index of the cell. 104 | * @param col The column index of the cell. 105 | * @param num The number to be placed in the cell. 106 | * @return True if the move is valid, false otherwise. 107 | */ 108 | public static boolean isValidMove(int[][] board, int row, int col, int num) { 109 | // Check if num is already present in the row or column 110 | for (int i = 0; i < 9; i++) { 111 | if (board[row][i] == num || board[i][col] == num) { 112 | return false; 113 | } 114 | } 115 | 116 | // Check if num is already present in the 3x3 grid 117 | int startRow = row - row % 3; 118 | int startCol = col - col % 3; 119 | for (int i = 0; i < 3; i++) { 120 | for (int j = 0; j < 3; j++) { 121 | if (board[startRow + i][startCol + j] == num) { 122 | return false; 123 | } 124 | } 125 | } 126 | 127 | // Return true if the move is valid 128 | return true; 129 | } 130 | 131 | /** 132 | * Prints the Sudoku puzzle board. 133 | * 134 | * @param board The Sudoku puzzle board. 135 | */ 136 | public static void printBoard(int[][] board) { 137 | for (int row = 0; row < 9; row++) { 138 | for (int col = 0; col < 9; col++) { 139 | System.out.print(board[row][col] + " "); 140 | } 141 | System.out.println(); 142 | } 143 | } 144 | } 145 | ``` --------------------------------------------------------------------------------