Sudoku is a game that challenges both the mind and the spirit of logic. It consists of a 9x9 grid that is partially filled with digits from 1 to 9. The objective is to fill the grid such that every row, every column, and each of the nine 3x3 subgrids contains all of the digits from 1 to 9 without repetitions. When it comes to solving these puzzles programmatically, recursion and backtracking are two powerful techniques that come into play. In this blog, we are going to explore how to build a Sudoku solver in Java using these concepts.
Backtracking is a technique for finding solutions to problems incrementally, by trying partial solutions and then removing those that fail to satisfy the conditions of the problem. In the context of Sudoku:
Consider a partially filled Sudoku grid:
5 3 . | . 7 . | . . .
6 . . | 1 9 5 | . . .
. 9 8 | . . . | . 6 .
------+-------+------
8 . . | . 6 . | . . 3
4 . . | 8 . 3 | . . 1
7 . . | . 2 . | . . 6
------+-------+------
. 6 . | . . . | 2 8 .
. . . | 4 1 9 | . . 5
. . . | . 8 . | . 7 9
Here, .
represents an empty cell. Our task is to fill it completely while adhering to the Sudoku rules.
Let's jump straight into the implementation. Below is the Java code for a Sudoku solver using backtracking:
public class SudokuSolver { private static final int SIZE = 9; public void solveSudoku(char[][] board) { backtrack(board); } private boolean backtrack(char[][] board) { for (int row = 0; row < SIZE; row++) { for (int col = 0; col < SIZE; col++) { if (board[row][col] == '.') { // Find an empty cell for (char num = '1'; num <= '9'; num++) { if (isValid(board, row, col, num)) { board[row][col] = num; // Place number if (backtrack(board)) { // Recur return true; // Sudoku solved } board[row][col] = '.'; // Backtrack } } return false; // If no number can be placed return false } } } return true; // All cells filled correctly } private boolean isValid(char[][] board, int row, int col, char num) { for (int i = 0; i < SIZE; i++) { if (board[row][i] == num || board[i][col] == num || board[row / 3 * 3 + i / 3][col / 3 * 3 + i % 3] == num) { return false; // Check row, column, and subgrid } } return true; // If valid } }
Data Structure: We are using a 2D char array to represent the Sudoku board.
Main Method: The solveSudoku
method kicks off the solving process.
Backtracking Function: The core of our logic is in the backtrack
method. It scans the board for empty cells and tries each number from 1 to 9.
Valid Placement Check: In the isValid
method, we ensure that a number doesn’t already exist in the current row, column, or the respective 3x3 subgrid.
This simple yet effective implementation allows us to solve Sudoku puzzles of varying difficulty by leveraging the elegance of recursion and the systematic approach of backtracking.
To test our solver, create a main
method as follows:
public static void main(String[] args) { char[][] board = { {'5', '3', '.', '.', '7', '.', '.', '.', '.'}, {'6', '.', '.', '1', '9', '5', '.', '.', '.'}, {'.', '9', '8', '.', '.', '.', '.', '6', '.'}, {'8', '.', '.', '.', '6', '.', '.', '.', '3'}, {'4', '.', '.', '8', '.', '3', '.', '.', '1'}, {'7', '.', '.', '.', '2', '.', '.', '.', '6'}, {'.', '6', '.', '.', '.', '.', '2', '8', '.'}, {'.', '.', '.', '4', '1', '9', '.', '.', '5'}, {'.', '.', '.', '.', '8', '.', '.', '7', '9'} }; SudokuSolver solver = new SudokuSolver(); solver.solveSudoku(board); for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[i].length; j++) { System.out.print(board[i][j] + " "); } System.out.println(); } }
When you run this code, it should fill the empty cells of the Sudoku puzzle, showcasing the power of recursion and backtracking.
Solving Sudoku puzzles using the principles of advanced recursion and backtracking in Java is not only fun but also a great way to polish your problem-solving abilities. By understanding and implementing these concepts, you can tackle complex algorithmic challenges head-on!
06/12/2024 | DSA
15/11/2024 | DSA
16/11/2024 | DSA
23/09/2024 | DSA
13/10/2024 | DSA
16/11/2024 | DSA
06/12/2024 | DSA
13/10/2024 | DSA
16/11/2024 | DSA
08/12/2024 | DSA
15/11/2024 | DSA