From 9cc1af8227b0dc5277b76432c42f4246808b42d5 Mon Sep 17 00:00:00 2001 From: harsha08-2k6 Date: Sat, 22 Nov 2025 11:23:03 +0530 Subject: [PATCH 1/5] =?UTF-8?q?Add=20Sudoku=20Solver=20using=20Backtrackin?= =?UTF-8?q?g=20algorithm-=20Implements=20depth-first=20backtracking=20to?= =?UTF-8?q?=20solve=209=C3=979=20Sudoku=20puzzles-=20Includes=20validation?= =?UTF-8?q?=20for=20rows,=20columns,=20and=203=C3=973=20subgrids-=20Provid?= =?UTF-8?q?es=20clean,=20modular=20implementation=20with=20helper=20method?= =?UTF-8?q?s-=20Includes=20comprehensive=20unit=20tests=20for=20solvable?= =?UTF-8?q?=20and=20unsolvable=20cases-=20Time=20Complexity:=20O(9^(n?= =?UTF-8?q?=C2=B2))=20in=20worst=20case-=20Space=20Complexity:=20O(n=C2=B2?= =?UTF-8?q?)=20for=20recursion=20stack?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backtracking/SudokuSolver.java | 199 ++++++++++-------- .../backtracking/SudokuSolverTest.java | 110 +++++++--- 2 files changed, 185 insertions(+), 124 deletions(-) diff --git a/src/main/java/com/thealgorithms/backtracking/SudokuSolver.java b/src/main/java/com/thealgorithms/backtracking/SudokuSolver.java index 543fe2d02b50..34600d7e179b 100644 --- a/src/main/java/com/thealgorithms/backtracking/SudokuSolver.java +++ b/src/main/java/com/thealgorithms/backtracking/SudokuSolver.java @@ -2,150 +2,135 @@ /** * Sudoku Solver using Backtracking Algorithm - * Solves a 9x9 Sudoku puzzle by filling empty cells with valid digits (1-9) - * - * @author Navadeep0007 + * + * This class solves a partially filled 9×9 Sudoku board by finding a valid arrangement + * where every row, column, and 3×3 subgrid contains digits 1-9 exactly once. + * + * Time Complexity: O(9^(n*n)) in worst case, where n=9 + * Space Complexity: O(n*n) for recursion stack + * + * @author TheAlgorithms */ -public final class SudokuSolver { +public class SudokuSolver { - private static final int GRID_SIZE = 9; + private static final int SIZE = 9; private static final int SUBGRID_SIZE = 3; - private static final int EMPTY_CELL = 0; - - private SudokuSolver() { - // Utility class, prevent instantiation - } + private static final int EMPTY = 0; /** - * Solves the Sudoku puzzle using backtracking - * - * @param board 9x9 Sudoku board with 0 representing empty cells - * @return true if puzzle is solved, false otherwise + * Solves the given Sudoku board using backtracking. + * Modifies the board in-place. + * + * @param board 9×9 2D array representing the Sudoku board (0 = empty cell) + * @return true if a valid solution exists, false otherwise */ public static boolean solveSudoku(int[][] board) { - if (board == null || board.length != GRID_SIZE) { - return false; - } - - for (int row = 0; row < GRID_SIZE; row++) { - if (board[row].length != GRID_SIZE) { - return false; - } - } - - return solve(board); - } + for (int row = 0; row < SIZE; row++) { + for (int col = 0; col < SIZE; col++) { + // Find an empty cell + if (board[row][col] == EMPTY) { + // Try digits 1-9 + for (int num = 1; num <= SIZE; num++) { + if (isValid(board, row, col, num)) { + // Place the number + board[row][col] = num; - /** - * Recursive helper method to solve the Sudoku puzzle - * - * @param board the Sudoku board - * @return true if solution is found, false otherwise - */ - private static boolean solve(int[][] board) { - for (int row = 0; row < GRID_SIZE; row++) { - for (int col = 0; col < GRID_SIZE; col++) { - if (board[row][col] == EMPTY_CELL) { - for (int number = 1; number <= GRID_SIZE; number++) { - if (isValidPlacement(board, row, col, number)) { - board[row][col] = number; - - if (solve(board)) { + // Recursively try to solve the rest + if (solveSudoku(board)) { return true; } - // Backtrack - board[row][col] = EMPTY_CELL; + // Backtrack if no solution found + board[row][col] = EMPTY; } } + // No valid number found, backtrack return false; } } } + // All cells filled successfully return true; } /** - * Checks if placing a number at given position is valid - * + * Checks if placing a number at a given position is valid. + * * @param board the Sudoku board * @param row row index * @param col column index - * @param number number to place (1-9) - * @return true if placement is valid, false otherwise + * @param num number to place (1-9) + * @return true if placement is valid */ - private static boolean isValidPlacement(int[][] board, int row, int col, int number) { - return !isNumberInRow(board, row, number) && !isNumberInColumn(board, col, number) && !isNumberInSubgrid(board, row, col, number); + private static boolean isValid(int[][] board, int row, int col, int num) { + // Check row constraint + if (!isRowValid(board, row, num)) { + return false; + } + + // Check column constraint + if (!isColumnValid(board, col, num)) { + return false; + } + + // Check 3×3 subgrid constraint + if (!isSubgridValid(board, row, col, num)) { + return false; + } + + return true; } /** - * Checks if number exists in the given row - * - * @param board the Sudoku board - * @param row row index - * @param number number to check - * @return true if number exists in row, false otherwise + * Checks if a number already exists in the given row. */ - private static boolean isNumberInRow(int[][] board, int row, int number) { - for (int col = 0; col < GRID_SIZE; col++) { - if (board[row][col] == number) { - return true; + private static boolean isRowValid(int[][] board, int row, int num) { + for (int col = 0; col < SIZE; col++) { + if (board[row][col] == num) { + return false; } } - return false; + return true; } /** - * Checks if number exists in the given column - * - * @param board the Sudoku board - * @param col column index - * @param number number to check - * @return true if number exists in column, false otherwise + * Checks if a number already exists in the given column. */ - private static boolean isNumberInColumn(int[][] board, int col, int number) { - for (int row = 0; row < GRID_SIZE; row++) { - if (board[row][col] == number) { - return true; + private static boolean isColumnValid(int[][] board, int col, int num) { + for (int row = 0; row < SIZE; row++) { + if (board[row][col] == num) { + return false; } } - return false; + return true; } /** - * Checks if number exists in the 3x3 subgrid - * - * @param board the Sudoku board - * @param row row index - * @param col column index - * @param number number to check - * @return true if number exists in subgrid, false otherwise + * Checks if a number already exists in the 3×3 subgrid. */ - private static boolean isNumberInSubgrid(int[][] board, int row, int col, int number) { - int subgridRowStart = row - row % SUBGRID_SIZE; - int subgridColStart = col - col % SUBGRID_SIZE; - - for (int i = subgridRowStart; i < subgridRowStart + SUBGRID_SIZE; i++) { - for (int j = subgridColStart; j < subgridColStart + SUBGRID_SIZE; j++) { - if (board[i][j] == number) { - return true; + private static boolean isSubgridValid(int[][] board, int row, int col, int num) { + int subgridRow = row - row % SUBGRID_SIZE; + int subgridCol = col - col % SUBGRID_SIZE; + + for (int i = subgridRow; i < subgridRow + SUBGRID_SIZE; i++) { + for (int j = subgridCol; j < subgridCol + SUBGRID_SIZE; j++) { + if (board[i][j] == num) { + return false; } } } - return false; + return true; } /** - * Prints the Sudoku board - * - * @param board the Sudoku board + * Prints the Sudoku board in a readable format. */ public static void printBoard(int[][] board) { - for (int row = 0; row < GRID_SIZE; row++) { + for (int row = 0; row < SIZE; row++) { if (row % SUBGRID_SIZE == 0 && row != 0) { System.out.println("-----------"); } - for (int col = 0; col < GRID_SIZE; col++) { + for (int col = 0; col < SIZE; col++) { if (col % SUBGRID_SIZE == 0 && col != 0) { System.out.print("|"); } @@ -154,4 +139,32 @@ public static void printBoard(int[][] board) { System.out.println(); } } -} + + /** + * Main method demonstrating Sudoku solver functionality. + */ + public static void main(String[] args) { + // Example Sudoku puzzle (0 = empty cell) + int[][] board = { + {5, 3, 0, 0, 7, 0, 0, 0, 0}, + {6, 0, 0, 1, 9, 5, 0, 0, 0}, + {0, 9, 8, 0, 0, 0, 0, 6, 0}, + {8, 0, 0, 0, 6, 0, 0, 0, 3}, + {4, 0, 0, 8, 0, 3, 0, 0, 1}, + {7, 0, 0, 0, 2, 0, 0, 0, 6}, + {0, 6, 0, 0, 0, 0, 2, 8, 0}, + {0, 0, 0, 4, 1, 9, 0, 0, 5}, + {0, 0, 0, 0, 8, 0, 0, 7, 9} + }; + + System.out.println("Sudoku Puzzle:"); + printBoard(board); + + if (solveSudoku(board)) { + System.out.println("\nSolved Sudoku:"); + printBoard(board); + } else { + System.out.println("\nNo solution exists for this Sudoku puzzle."); + } + } +} \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/backtracking/SudokuSolverTest.java b/src/test/java/com/thealgorithms/backtracking/SudokuSolverTest.java index 75d3eae08629..47ba72b84c82 100644 --- a/src/test/java/com/thealgorithms/backtracking/SudokuSolverTest.java +++ b/src/test/java/com/thealgorithms/backtracking/SudokuSolverTest.java @@ -1,53 +1,101 @@ package com.thealgorithms.backtracking; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; - import org.junit.jupiter.api.Test; -class SudokuSolverTest { +public class SudokuSolverTest { @Test - void testSolveSudokuEasyPuzzle() { - int[][] board = {{5, 3, 0, 0, 7, 0, 0, 0, 0}, {6, 0, 0, 1, 9, 5, 0, 0, 0}, {0, 9, 8, 0, 0, 0, 0, 6, 0}, {8, 0, 0, 0, 6, 0, 0, 0, 3}, {4, 0, 0, 8, 0, 3, 0, 0, 1}, {7, 0, 0, 0, 2, 0, 0, 0, 6}, {0, 6, 0, 0, 0, 0, 2, 8, 0}, {0, 0, 0, 4, 1, 9, 0, 0, 5}, {0, 0, 0, 0, 8, 0, 0, 7, 9}}; - - assertTrue(SudokuSolver.solveSudoku(board)); - - int[][] expected = {{5, 3, 4, 6, 7, 8, 9, 1, 2}, {6, 7, 2, 1, 9, 5, 3, 4, 8}, {1, 9, 8, 3, 4, 2, 5, 6, 7}, {8, 5, 9, 7, 6, 1, 4, 2, 3}, {4, 2, 6, 8, 5, 3, 7, 9, 1}, {7, 1, 3, 9, 2, 4, 8, 5, 6}, {9, 6, 1, 5, 3, 7, 2, 8, 4}, {2, 8, 7, 4, 1, 9, 6, 3, 5}, {3, 4, 5, 2, 8, 6, 1, 7, 9}}; + public void testSolvableSudoku() { + int[][] board = { + {5, 3, 0, 0, 7, 0, 0, 0, 0}, + {6, 0, 0, 1, 9, 5, 0, 0, 0}, + {0, 9, 8, 0, 0, 0, 0, 6, 0}, + {8, 0, 0, 0, 6, 0, 0, 0, 3}, + {4, 0, 0, 8, 0, 3, 0, 0, 1}, + {7, 0, 0, 0, 2, 0, 0, 0, 6}, + {0, 6, 0, 0, 0, 0, 2, 8, 0}, + {0, 0, 0, 4, 1, 9, 0, 0, 5}, + {0, 0, 0, 0, 8, 0, 0, 7, 9} + }; - assertArrayEquals(expected, board); + assertTrue(SudokuSolver.solveSudoku(board), "Sudoku should be solvable"); + assertBoardValid(board); } @Test - void testSolveSudokuHardPuzzle() { - int[][] board = {{0, 0, 0, 0, 0, 0, 6, 8, 0}, {0, 0, 0, 0, 7, 3, 0, 0, 9}, {3, 0, 9, 0, 0, 0, 0, 4, 5}, {4, 9, 0, 0, 0, 0, 0, 0, 0}, {8, 0, 3, 0, 5, 0, 9, 0, 2}, {0, 0, 0, 0, 0, 0, 0, 3, 6}, {9, 6, 0, 0, 0, 0, 3, 0, 8}, {7, 0, 0, 6, 8, 0, 0, 0, 0}, {0, 2, 8, 0, 0, 0, 0, 0, 0}}; + public void testUnsolvableSudoku() { + int[][] board = { + {5, 3, 5, 0, 7, 0, 0, 0, 0}, // Row has duplicate 5s + {6, 0, 0, 1, 9, 5, 0, 0, 0}, + {0, 9, 8, 0, 0, 0, 0, 6, 0}, + {8, 0, 0, 0, 6, 0, 0, 0, 3}, + {4, 0, 0, 8, 0, 3, 0, 0, 1}, + {7, 0, 0, 0, 2, 0, 0, 0, 6}, + {0, 6, 0, 0, 0, 0, 2, 8, 0}, + {0, 0, 0, 4, 1, 9, 0, 0, 5}, + {0, 0, 0, 0, 8, 0, 0, 7, 9} + }; - assertTrue(SudokuSolver.solveSudoku(board)); + assertFalse(SudokuSolver.solveSudoku(board), "Sudoku should not be solvable"); } @Test - void testSolveSudokuAlreadySolved() { - int[][] board = {{5, 3, 4, 6, 7, 8, 9, 1, 2}, {6, 7, 2, 1, 9, 5, 3, 4, 8}, {1, 9, 8, 3, 4, 2, 5, 6, 7}, {8, 5, 9, 7, 6, 1, 4, 2, 3}, {4, 2, 6, 8, 5, 3, 7, 9, 1}, {7, 1, 3, 9, 2, 4, 8, 5, 6}, {9, 6, 1, 5, 3, 7, 2, 8, 4}, {2, 8, 7, 4, 1, 9, 6, 3, 5}, {3, 4, 5, 2, 8, 6, 1, 7, 9}}; + public void testCompleteBoard() { + int[][] board = { + {5, 3, 4, 6, 7, 8, 9, 1, 2}, + {6, 7, 2, 1, 9, 5, 3, 4, 8}, + {1, 9, 8, 3, 4, 2, 5, 6, 7}, + {8, 5, 9, 7, 6, 1, 4, 2, 3}, + {4, 2, 6, 8, 5, 3, 7, 9, 1}, + {7, 1, 3, 9, 2, 4, 8, 5, 6}, + {9, 6, 1, 5, 3, 7, 2, 8, 4}, + {2, 8, 7, 4, 1, 9, 6, 3, 5}, + {3, 4, 5, 2, 8, 6, 1, 7, 9} + }; - assertTrue(SudokuSolver.solveSudoku(board)); + assertTrue(SudokuSolver.solveSudoku(board), "Already solved Sudoku should return true"); + assertBoardValid(board); } - @Test - void testSolveSudokuInvalidSize() { - int[][] board = {{1, 2, 3}, {4, 5, 6}}; - assertFalse(SudokuSolver.solveSudoku(board)); - } + /** + * Helper method to validate that the board is correctly solved. + */ + private void assertBoardValid(int[][] board) { + // Check rows + for (int row = 0; row < 9; row++) { + boolean[] seen = new boolean[10]; + for (int col = 0; col < 9; col++) { + int num = board[row][col]; + assertTrue(num >= 1 && num <= 9, "Invalid number in board"); + assertFalse(seen[num], "Duplicate in row " + row); + seen[num] = true; + } + } - @Test - void testSolveSudokuNullBoard() { - assertFalse(SudokuSolver.solveSudoku(null)); - } - - @Test - void testSolveSudokuEmptyBoard() { - int[][] board = {{0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}}; + // Check columns + for (int col = 0; col < 9; col++) { + boolean[] seen = new boolean[10]; + for (int row = 0; row < 9; row++) { + int num = board[row][col]; + assertFalse(seen[num], "Duplicate in column " + col); + seen[num] = true; + } + } - assertTrue(SudokuSolver.solveSudoku(board)); + // Check 3×3 subgrids + for (int boxRow = 0; boxRow < 3; boxRow++) { + for (int boxCol = 0; boxCol < 3; boxCol++) { + boolean[] seen = new boolean[10]; + for (int i = boxRow * 3; i < boxRow * 3 + 3; i++) { + for (int j = boxCol * 3; j < boxCol * 3 + 3; j++) { + int num = board[i][j]; + assertFalse(seen[num], "Duplicate in subgrid"); + seen[num] = true; + } + } + } + } } -} +} \ No newline at end of file From 8d51415ed8dc530a3ed53d521f739de7538aa6ad Mon Sep 17 00:00:00 2001 From: harsha08-2k6 Date: Sat, 22 Nov 2025 11:30:44 +0530 Subject: [PATCH 2/5] =?UTF-8?q?Add=20Sudoku=20Solver=20using=20Backtrackin?= =?UTF-8?q?g=20algorithm-=20Implements=20depth-first=20backtracking=20to?= =?UTF-8?q?=20solve=209=C3=979=20Sudoku=20puzzles-=20Includes=20validation?= =?UTF-8?q?=20for=20rows,=20columns,=20and=203=C3=973=20subgrids-=20Provid?= =?UTF-8?q?es=20clean,=20modular=20implementation=20with=20helper=20method?= =?UTF-8?q?s-=20Includes=20comprehensive=20unit=20tests=20for=20solvable?= =?UTF-8?q?=20and=20unsolvable=20cases-=20Time=20Complexity:=20O(9^(n?= =?UTF-8?q?=C2=B2))=20in=20worst=20case-=20Space=20Complexity:=20O(n=C2=B2?= =?UTF-8?q?)=20for=20recursion=20stack?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backtracking/SudokuSolver.java | 316 ++++++++++-------- .../backtracking/SudokuSolverTest.java | 169 +++++----- 2 files changed, 258 insertions(+), 227 deletions(-) diff --git a/src/main/java/com/thealgorithms/backtracking/SudokuSolver.java b/src/main/java/com/thealgorithms/backtracking/SudokuSolver.java index 34600d7e179b..abb64b3eee3c 100644 --- a/src/main/java/com/thealgorithms/backtracking/SudokuSolver.java +++ b/src/main/java/com/thealgorithms/backtracking/SudokuSolver.java @@ -2,169 +2,195 @@ /** * Sudoku Solver using Backtracking Algorithm - * - * This class solves a partially filled 9×9 Sudoku board by finding a valid arrangement - * where every row, column, and 3×3 subgrid contains digits 1-9 exactly once. - * + * + * This class solves a partially filled 9×9 Sudoku board by finding a valid + * arrangement where every row, column, and 3×3 subgrid contains digits 1-9 + * exactly once. + * + * Algorithm References: + * - Backtracking: https://en.wikipedia.org/wiki/Backtracking + * - Sudoku: https://en.wikipedia.org/wiki/Sudoku + * * Time Complexity: O(9^(n*n)) in worst case, where n=9 * Space Complexity: O(n*n) for recursion stack - * + * * @author TheAlgorithms */ public class SudokuSolver { - private static final int SIZE = 9; - private static final int SUBGRID_SIZE = 3; - private static final int EMPTY = 0; - - /** - * Solves the given Sudoku board using backtracking. - * Modifies the board in-place. - * - * @param board 9×9 2D array representing the Sudoku board (0 = empty cell) - * @return true if a valid solution exists, false otherwise - */ - public static boolean solveSudoku(int[][] board) { - for (int row = 0; row < SIZE; row++) { - for (int col = 0; col < SIZE; col++) { - // Find an empty cell - if (board[row][col] == EMPTY) { - // Try digits 1-9 - for (int num = 1; num <= SIZE; num++) { - if (isValid(board, row, col, num)) { - // Place the number - board[row][col] = num; - - // Recursively try to solve the rest - if (solveSudoku(board)) { - return true; - } - - // Backtrack if no solution found - board[row][col] = EMPTY; - } - } - // No valid number found, backtrack - return false; - } + private static final int SIZE = 9; + private static final int SUBGRID_SIZE = 3; + private static final int EMPTY = 0; + + /** + * Solves the given Sudoku board using backtracking. Modifies the board + * in-place. + * + * @param board 9×9 2D array representing the Sudoku board (0 = empty cell) + * @return true if a valid solution exists, false otherwise + */ + public static boolean solveSudoku(int[][] board) { + for (int row = 0; row < SIZE; row++) { + for (int col = 0; col < SIZE; col++) { + // Find an empty cell + if (board[row][col] == EMPTY) { + // Try digits 1-9 + for (int num = 1; num <= SIZE; num++) { + if (isValid(board, row, col, num)) { + // Place the number + board[row][col] = num; + + // Recursively try to solve the rest + if (solveSudoku(board)) { + return true; + } + + // Backtrack if no solution found + board[row][col] = EMPTY; } + } + // No valid number found, backtrack + return false; } - // All cells filled successfully - return true; + } } - - /** - * Checks if placing a number at a given position is valid. - * - * @param board the Sudoku board - * @param row row index - * @param col column index - * @param num number to place (1-9) - * @return true if placement is valid - */ - private static boolean isValid(int[][] board, int row, int col, int num) { - // Check row constraint - if (!isRowValid(board, row, num)) { - return false; - } - - // Check column constraint - if (!isColumnValid(board, col, num)) { - return false; - } - - // Check 3×3 subgrid constraint - if (!isSubgridValid(board, row, col, num)) { - return false; - } - - return true; + // All cells filled successfully + return true; + } + + /** + * Checks if placing a number at a given position is valid. + * + * @param board the Sudoku board + * @param row row index + * @param col column index + * @param num number to place (1-9) + * @return true if placement is valid + */ + private static boolean isValid(int[][] board, int row, int col, int num) { + // Check row constraint + if (!isRowValid(board, row, num)) { + return false; } - /** - * Checks if a number already exists in the given row. - */ - private static boolean isRowValid(int[][] board, int row, int num) { - for (int col = 0; col < SIZE; col++) { - if (board[row][col] == num) { - return false; - } - } - return true; + // Check column constraint + if (!isColumnValid(board, col, num)) { + return false; } - /** - * Checks if a number already exists in the given column. - */ - private static boolean isColumnValid(int[][] board, int col, int num) { - for (int row = 0; row < SIZE; row++) { - if (board[row][col] == num) { - return false; - } - } - return true; + // Check 3×3 subgrid constraint + if (!isSubgridValid(board, row, col, num)) { + return false; } - /** - * Checks if a number already exists in the 3×3 subgrid. - */ - private static boolean isSubgridValid(int[][] board, int row, int col, int num) { - int subgridRow = row - row % SUBGRID_SIZE; - int subgridCol = col - col % SUBGRID_SIZE; - - for (int i = subgridRow; i < subgridRow + SUBGRID_SIZE; i++) { - for (int j = subgridCol; j < subgridCol + SUBGRID_SIZE; j++) { - if (board[i][j] == num) { - return false; - } - } - } - return true; + return true; + } + + /** + * Checks if a number already exists in the given row. + * + * @param board the Sudoku board + * @param row row index + * @param num number to check + * @return true if number is not in row + */ + private static boolean isRowValid(int[][] board, int row, int num) { + for (int col = 0; col < SIZE; col++) { + if (board[row][col] == num) { + return false; + } } - - /** - * Prints the Sudoku board in a readable format. - */ - public static void printBoard(int[][] board) { - for (int row = 0; row < SIZE; row++) { - if (row % SUBGRID_SIZE == 0 && row != 0) { - System.out.println("-----------"); - } - for (int col = 0; col < SIZE; col++) { - if (col % SUBGRID_SIZE == 0 && col != 0) { - System.out.print("|"); - } - System.out.print(board[row][col]); - } - System.out.println(); + return true; + } + + /** + * Checks if a number already exists in the given column. + * + * @param board the Sudoku board + * @param col column index + * @param num number to check + * @return true if number is not in column + */ + private static boolean isColumnValid(int[][] board, int col, int num) { + for (int row = 0; row < SIZE; row++) { + if (board[row][col] == num) { + return false; + } + } + return true; + } + + /** + * Checks if a number already exists in the 3×3 subgrid. + * + * @param board the Sudoku board + * @param row row index + * @param col column index + * @param num number to check + * @return true if number is not in subgrid + */ + private static boolean isSubgridValid(int[][] board, int row, int col, + int num) { + int subgridRow = row - row % SUBGRID_SIZE; + int subgridCol = col - col % SUBGRID_SIZE; + + for (int i = subgridRow; i < subgridRow + SUBGRID_SIZE; i++) { + for (int j = subgridCol; j < subgridCol + SUBGRID_SIZE; j++) { + if (board[i][j] == num) { + return false; } + } } - - /** - * Main method demonstrating Sudoku solver functionality. - */ - public static void main(String[] args) { - // Example Sudoku puzzle (0 = empty cell) - int[][] board = { - {5, 3, 0, 0, 7, 0, 0, 0, 0}, - {6, 0, 0, 1, 9, 5, 0, 0, 0}, - {0, 9, 8, 0, 0, 0, 0, 6, 0}, - {8, 0, 0, 0, 6, 0, 0, 0, 3}, - {4, 0, 0, 8, 0, 3, 0, 0, 1}, - {7, 0, 0, 0, 2, 0, 0, 0, 6}, - {0, 6, 0, 0, 0, 0, 2, 8, 0}, - {0, 0, 0, 4, 1, 9, 0, 0, 5}, - {0, 0, 0, 0, 8, 0, 0, 7, 9} - }; - - System.out.println("Sudoku Puzzle:"); - printBoard(board); - - if (solveSudoku(board)) { - System.out.println("\nSolved Sudoku:"); - printBoard(board); - } else { - System.out.println("\nNo solution exists for this Sudoku puzzle."); + return true; + } + + /** + * Prints the Sudoku board in a readable format. + * + * @param board the Sudoku board to print + */ + public static void printBoard(int[][] board) { + for (int row = 0; row < SIZE; row++) { + if (row % SUBGRID_SIZE == 0 && row != 0) { + System.out.println("-----------"); + } + for (int col = 0; col < SIZE; col++) { + if (col % SUBGRID_SIZE == 0 && col != 0) { + System.out.print("|"); } + System.out.print(board[row][col]); + } + System.out.println(); + } + } + + /** + * Main method demonstrating Sudoku solver functionality. + * + * @param args command line arguments (not used) + */ + public static void main(String[] args) { + // Example Sudoku puzzle (0 = empty cell) + int[][] board = { + {5, 3, 0, 0, 7, 0, 0, 0, 0}, + {6, 0, 0, 1, 9, 5, 0, 0, 0}, + {0, 9, 8, 0, 0, 0, 0, 6, 0}, + {8, 0, 0, 0, 6, 0, 0, 0, 3}, + {4, 0, 0, 8, 0, 3, 0, 0, 1}, + {7, 0, 0, 0, 2, 0, 0, 0, 6}, + {0, 6, 0, 0, 0, 0, 2, 8, 0}, + {0, 0, 0, 4, 1, 9, 0, 0, 5}, + {0, 0, 0, 0, 8, 0, 0, 7, 9} + }; + + System.out.println("Sudoku Puzzle:"); + printBoard(board); + + if (solveSudoku(board)) { + System.out.println("\nSolved Sudoku:"); + printBoard(board); + } else { + System.out.println("\nNo solution exists for this Sudoku puzzle."); } + } } \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/backtracking/SudokuSolverTest.java b/src/test/java/com/thealgorithms/backtracking/SudokuSolverTest.java index 47ba72b84c82..9f7903628e28 100644 --- a/src/test/java/com/thealgorithms/backtracking/SudokuSolverTest.java +++ b/src/test/java/com/thealgorithms/backtracking/SudokuSolverTest.java @@ -6,96 +6,101 @@ public class SudokuSolverTest { - @Test - public void testSolvableSudoku() { - int[][] board = { - {5, 3, 0, 0, 7, 0, 0, 0, 0}, - {6, 0, 0, 1, 9, 5, 0, 0, 0}, - {0, 9, 8, 0, 0, 0, 0, 6, 0}, - {8, 0, 0, 0, 6, 0, 0, 0, 3}, - {4, 0, 0, 8, 0, 3, 0, 0, 1}, - {7, 0, 0, 0, 2, 0, 0, 0, 6}, - {0, 6, 0, 0, 0, 0, 2, 8, 0}, - {0, 0, 0, 4, 1, 9, 0, 0, 5}, - {0, 0, 0, 0, 8, 0, 0, 7, 9} - }; + @Test + public void testSolvableSudoku() { + int[][] board = { + {5, 3, 0, 0, 7, 0, 0, 0, 0}, + {6, 0, 0, 1, 9, 5, 0, 0, 0}, + {0, 9, 8, 0, 0, 0, 0, 6, 0}, + {8, 0, 0, 0, 6, 0, 0, 0, 3}, + {4, 0, 0, 8, 0, 3, 0, 0, 1}, + {7, 0, 0, 0, 2, 0, 0, 0, 6}, + {0, 6, 0, 0, 0, 0, 2, 8, 0}, + {0, 0, 0, 4, 1, 9, 0, 0, 5}, + {0, 0, 0, 0, 8, 0, 0, 7, 9} + }; - assertTrue(SudokuSolver.solveSudoku(board), "Sudoku should be solvable"); - assertBoardValid(board); - } + assertTrue(SudokuSolver.solveSudoku(board), + "Sudoku should be solvable"); + assertBoardValid(board); + } - @Test - public void testUnsolvableSudoku() { - int[][] board = { - {5, 3, 5, 0, 7, 0, 0, 0, 0}, // Row has duplicate 5s - {6, 0, 0, 1, 9, 5, 0, 0, 0}, - {0, 9, 8, 0, 0, 0, 0, 6, 0}, - {8, 0, 0, 0, 6, 0, 0, 0, 3}, - {4, 0, 0, 8, 0, 3, 0, 0, 1}, - {7, 0, 0, 0, 2, 0, 0, 0, 6}, - {0, 6, 0, 0, 0, 0, 2, 8, 0}, - {0, 0, 0, 4, 1, 9, 0, 0, 5}, - {0, 0, 0, 0, 8, 0, 0, 7, 9} - }; + @Test + public void testUnsolvableSudoku() { + int[][] board = { + {5, 3, 5, 0, 7, 0, 0, 0, 0}, // Row has duplicate 5s + {6, 0, 0, 1, 9, 5, 0, 0, 0}, + {0, 9, 8, 0, 0, 0, 0, 6, 0}, + {8, 0, 0, 0, 6, 0, 0, 0, 3}, + {4, 0, 0, 8, 0, 3, 0, 0, 1}, + {7, 0, 0, 0, 2, 0, 0, 0, 6}, + {0, 6, 0, 0, 0, 0, 2, 8, 0}, + {0, 0, 0, 4, 1, 9, 0, 0, 5}, + {0, 0, 0, 0, 8, 0, 0, 7, 9} + }; - assertFalse(SudokuSolver.solveSudoku(board), "Sudoku should not be solvable"); - } + assertFalse(SudokuSolver.solveSudoku(board), + "Sudoku should not be solvable"); + } - @Test - public void testCompleteBoard() { - int[][] board = { - {5, 3, 4, 6, 7, 8, 9, 1, 2}, - {6, 7, 2, 1, 9, 5, 3, 4, 8}, - {1, 9, 8, 3, 4, 2, 5, 6, 7}, - {8, 5, 9, 7, 6, 1, 4, 2, 3}, - {4, 2, 6, 8, 5, 3, 7, 9, 1}, - {7, 1, 3, 9, 2, 4, 8, 5, 6}, - {9, 6, 1, 5, 3, 7, 2, 8, 4}, - {2, 8, 7, 4, 1, 9, 6, 3, 5}, - {3, 4, 5, 2, 8, 6, 1, 7, 9} - }; + @Test + public void testCompleteBoard() { + int[][] board = { + {5, 3, 4, 6, 7, 8, 9, 1, 2}, + {6, 7, 2, 1, 9, 5, 3, 4, 8}, + {1, 9, 8, 3, 4, 2, 5, 6, 7}, + {8, 5, 9, 7, 6, 1, 4, 2, 3}, + {4, 2, 6, 8, 5, 3, 7, 9, 1}, + {7, 1, 3, 9, 2, 4, 8, 5, 6}, + {9, 6, 1, 5, 3, 7, 2, 8, 4}, + {2, 8, 7, 4, 1, 9, 6, 3, 5}, + {3, 4, 5, 2, 8, 6, 1, 7, 9} + }; - assertTrue(SudokuSolver.solveSudoku(board), "Already solved Sudoku should return true"); - assertBoardValid(board); - } + assertTrue(SudokuSolver.solveSudoku(board), + "Already solved Sudoku should return true"); + assertBoardValid(board); + } - /** - * Helper method to validate that the board is correctly solved. - */ - private void assertBoardValid(int[][] board) { - // Check rows - for (int row = 0; row < 9; row++) { - boolean[] seen = new boolean[10]; - for (int col = 0; col < 9; col++) { - int num = board[row][col]; - assertTrue(num >= 1 && num <= 9, "Invalid number in board"); - assertFalse(seen[num], "Duplicate in row " + row); - seen[num] = true; - } - } + /** + * Helper method to validate that the board is correctly solved. + * + * @param board the solved Sudoku board + */ + private void assertBoardValid(int[][] board) { + // Check rows + for (int row = 0; row < 9; row++) { + boolean[] seen = new boolean[10]; + for (int col = 0; col < 9; col++) { + int num = board[row][col]; + assertTrue(num >= 1 && num <= 9, "Invalid number in board"); + assertFalse(seen[num], "Duplicate in row " + row); + seen[num] = true; + } + } - // Check columns - for (int col = 0; col < 9; col++) { - boolean[] seen = new boolean[10]; - for (int row = 0; row < 9; row++) { - int num = board[row][col]; - assertFalse(seen[num], "Duplicate in column " + col); - seen[num] = true; - } - } + // Check columns + for (int col = 0; col < 9; col++) { + boolean[] seen = new boolean[10]; + for (int row = 0; row < 9; row++) { + int num = board[row][col]; + assertFalse(seen[num], "Duplicate in column " + col); + seen[num] = true; + } + } - // Check 3×3 subgrids - for (int boxRow = 0; boxRow < 3; boxRow++) { - for (int boxCol = 0; boxCol < 3; boxCol++) { - boolean[] seen = new boolean[10]; - for (int i = boxRow * 3; i < boxRow * 3 + 3; i++) { - for (int j = boxCol * 3; j < boxCol * 3 + 3; j++) { - int num = board[i][j]; - assertFalse(seen[num], "Duplicate in subgrid"); - seen[num] = true; - } - } - } + // Check 3×3 subgrids + for (int boxRow = 0; boxRow < 3; boxRow++) { + for (int boxCol = 0; boxCol < 3; boxCol++) { + boolean[] seen = new boolean[10]; + for (int i = boxRow * 3; i < boxRow * 3 + 3; i++) { + for (int j = boxCol * 3; j < boxCol * 3 + 3; j++) { + int num = board[i][j]; + assertFalse(seen[num], "Duplicate in subgrid"); + seen[num] = true; + } } + } } + } } \ No newline at end of file From a9eee457d022b4c5f5969f3e5d6f8d4d414982b4 Mon Sep 17 00:00:00 2001 From: harsha08-2k6 Date: Sat, 22 Nov 2025 11:42:22 +0530 Subject: [PATCH 3/5] Add Sudoku Solver using Backtracking algorithm --- .../backtracking/SudokuSolver.java | 294 +++++++----------- .../backtracking/SudokuSolverTest.java | 168 +++++----- 2 files changed, 199 insertions(+), 263 deletions(-) diff --git a/src/main/java/com/thealgorithms/backtracking/SudokuSolver.java b/src/main/java/com/thealgorithms/backtracking/SudokuSolver.java index abb64b3eee3c..7b9a74913a95 100644 --- a/src/main/java/com/thealgorithms/backtracking/SudokuSolver.java +++ b/src/main/java/com/thealgorithms/backtracking/SudokuSolver.java @@ -1,196 +1,140 @@ package com.thealgorithms.backtracking; /** - * Sudoku Solver using Backtracking Algorithm + * Sudoku Solver using Backtracking Algorithm. * - * This class solves a partially filled 9×9 Sudoku board by finding a valid - * arrangement where every row, column, and 3×3 subgrid contains digits 1-9 - * exactly once. - * - * Algorithm References: - * - Backtracking: https://en.wikipedia.org/wiki/Backtracking - * - Sudoku: https://en.wikipedia.org/wiki/Sudoku - * - * Time Complexity: O(9^(n*n)) in worst case, where n=9 - * Space Complexity: O(n*n) for recursion stack - * - * @author TheAlgorithms + * Solves a 9×9 Sudoku grid by filling empty cells (0) such that every row, + * column, and 3×3 subgrid contains all digits 1–9 exactly once. */ public class SudokuSolver { - private static final int SIZE = 9; - private static final int SUBGRID_SIZE = 3; - private static final int EMPTY = 0; - - /** - * Solves the given Sudoku board using backtracking. Modifies the board - * in-place. - * - * @param board 9×9 2D array representing the Sudoku board (0 = empty cell) - * @return true if a valid solution exists, false otherwise - */ - public static boolean solveSudoku(int[][] board) { - for (int row = 0; row < SIZE; row++) { - for (int col = 0; col < SIZE; col++) { - // Find an empty cell - if (board[row][col] == EMPTY) { - // Try digits 1-9 - for (int num = 1; num <= SIZE; num++) { - if (isValid(board, row, col, num)) { - // Place the number - board[row][col] = num; - - // Recursively try to solve the rest - if (solveSudoku(board)) { - return true; - } - - // Backtrack if no solution found - board[row][col] = EMPTY; - } - } - // No valid number found, backtrack - return false; + private static final int SIZE = 9; + private static final int SUBGRID = 3; + private static final int EMPTY = 0; + + /** + * Public API: Validates the initial board and triggers the solver. + * + * @param board 9×9 Sudoku grid + * @return true if solvable + */ + public static boolean solveSudoku(int[][] board) { + if (!isInitialBoardValid(board)) { + return false; } - } - } - // All cells filled successfully - return true; - } - - /** - * Checks if placing a number at a given position is valid. - * - * @param board the Sudoku board - * @param row row index - * @param col column index - * @param num number to place (1-9) - * @return true if placement is valid - */ - private static boolean isValid(int[][] board, int row, int col, int num) { - // Check row constraint - if (!isRowValid(board, row, num)) { - return false; + return solve(board); } - // Check column constraint - if (!isColumnValid(board, col, num)) { - return false; + /** + * Solves Sudoku recursively using backtracking. + */ + private static boolean solve(int[][] board) { + for (int row = 0; row < SIZE; row++) { + for (int col = 0; col < SIZE; col++) { + + if (board[row][col] == EMPTY) { + + for (int num = 1; num <= SIZE; num++) { + + if (isValid(board, row, col, num)) { + board[row][col] = num; + + if (solve(board)) { + return true; + } + + board[row][col] = EMPTY; + } + } + + return false; + } + } + } + return true; } - // Check 3×3 subgrid constraint - if (!isSubgridValid(board, row, col, num)) { - return false; + /** + * Validates initial puzzle (checks duplicates in rows/cols/subgrids). + */ + public static boolean isInitialBoardValid(int[][] board) { + for (int row = 0; row < SIZE; row++) { + for (int col = 0; col < SIZE; col++) { + + int num = board[row][col]; + + if (num != EMPTY) { + board[row][col] = EMPTY; + + if (!isValid(board, row, col, num)) { + board[row][col] = num; + return false; + } + + board[row][col] = num; + } + } + } + return true; } - return true; - } - - /** - * Checks if a number already exists in the given row. - * - * @param board the Sudoku board - * @param row row index - * @param num number to check - * @return true if number is not in row - */ - private static boolean isRowValid(int[][] board, int row, int num) { - for (int col = 0; col < SIZE; col++) { - if (board[row][col] == num) { - return false; - } + private static boolean isValid(int[][] board, int row, int col, int num) { + return isRowOk(board, row, num) + && isColOk(board, col, num) + && isSubgridOk(board, row, col, num); } - return true; - } - - /** - * Checks if a number already exists in the given column. - * - * @param board the Sudoku board - * @param col column index - * @param num number to check - * @return true if number is not in column - */ - private static boolean isColumnValid(int[][] board, int col, int num) { - for (int row = 0; row < SIZE; row++) { - if (board[row][col] == num) { - return false; - } + + private static boolean isRowOk(int[][] board, int row, int num) { + for (int col = 0; col < SIZE; col++) { + if (board[row][col] == num) { + return false; + } + } + return true; } - return true; - } - - /** - * Checks if a number already exists in the 3×3 subgrid. - * - * @param board the Sudoku board - * @param row row index - * @param col column index - * @param num number to check - * @return true if number is not in subgrid - */ - private static boolean isSubgridValid(int[][] board, int row, int col, - int num) { - int subgridRow = row - row % SUBGRID_SIZE; - int subgridCol = col - col % SUBGRID_SIZE; - - for (int i = subgridRow; i < subgridRow + SUBGRID_SIZE; i++) { - for (int j = subgridCol; j < subgridCol + SUBGRID_SIZE; j++) { - if (board[i][j] == num) { - return false; + + private static boolean isColOk(int[][] board, int col, int num) { + for (int row = 0; row < SIZE; row++) { + if (board[row][col] == num) { + return false; + } } - } + return true; } - return true; - } - - /** - * Prints the Sudoku board in a readable format. - * - * @param board the Sudoku board to print - */ - public static void printBoard(int[][] board) { - for (int row = 0; row < SIZE; row++) { - if (row % SUBGRID_SIZE == 0 && row != 0) { - System.out.println("-----------"); - } - for (int col = 0; col < SIZE; col++) { - if (col % SUBGRID_SIZE == 0 && col != 0) { - System.out.print("|"); + + private static boolean isSubgridOk(int[][] board, int row, int col, int num) { + int startRow = row - (row % SUBGRID); + int startCol = col - (col % SUBGRID); + + for (int i = startRow; i < startRow + SUBGRID; i++) { + for (int j = startCol; j < startCol + SUBGRID; j++) { + if (board[i][j] == num) { + return false; + } + } } - System.out.print(board[row][col]); - } - System.out.println(); + return true; } - } - - /** - * Main method demonstrating Sudoku solver functionality. - * - * @param args command line arguments (not used) - */ - public static void main(String[] args) { - // Example Sudoku puzzle (0 = empty cell) - int[][] board = { - {5, 3, 0, 0, 7, 0, 0, 0, 0}, - {6, 0, 0, 1, 9, 5, 0, 0, 0}, - {0, 9, 8, 0, 0, 0, 0, 6, 0}, - {8, 0, 0, 0, 6, 0, 0, 0, 3}, - {4, 0, 0, 8, 0, 3, 0, 0, 1}, - {7, 0, 0, 0, 2, 0, 0, 0, 6}, - {0, 6, 0, 0, 0, 0, 2, 8, 0}, - {0, 0, 0, 4, 1, 9, 0, 0, 5}, - {0, 0, 0, 0, 8, 0, 0, 7, 9} - }; - - System.out.println("Sudoku Puzzle:"); - printBoard(board); - - if (solveSudoku(board)) { - System.out.println("\nSolved Sudoku:"); - printBoard(board); - } else { - System.out.println("\nNo solution exists for this Sudoku puzzle."); + + /** + * Utility: Print Sudoku grid. + */ + public static void printBoard(int[][] board) { + for (int row = 0; row < SIZE; row++) { + + if (row % SUBGRID == 0 && row != 0) { + System.out.println("-----------"); + } + + for (int col = 0; col < SIZE; col++) { + + if (col % SUBGRID == 0 && col != 0) { + System.out.print("|"); + } + + System.out.print(board[row][col]); + } + System.out.println(); + } } - } -} \ No newline at end of file +} diff --git a/src/test/java/com/thealgorithms/backtracking/SudokuSolverTest.java b/src/test/java/com/thealgorithms/backtracking/SudokuSolverTest.java index 9f7903628e28..1cfb1ffa06d7 100644 --- a/src/test/java/com/thealgorithms/backtracking/SudokuSolverTest.java +++ b/src/test/java/com/thealgorithms/backtracking/SudokuSolverTest.java @@ -6,101 +6,93 @@ public class SudokuSolverTest { - @Test - public void testSolvableSudoku() { - int[][] board = { - {5, 3, 0, 0, 7, 0, 0, 0, 0}, - {6, 0, 0, 1, 9, 5, 0, 0, 0}, - {0, 9, 8, 0, 0, 0, 0, 6, 0}, - {8, 0, 0, 0, 6, 0, 0, 0, 3}, - {4, 0, 0, 8, 0, 3, 0, 0, 1}, - {7, 0, 0, 0, 2, 0, 0, 0, 6}, - {0, 6, 0, 0, 0, 0, 2, 8, 0}, - {0, 0, 0, 4, 1, 9, 0, 0, 5}, - {0, 0, 0, 0, 8, 0, 0, 7, 9} - }; + @Test + public void testSolvableSudoku() { + int[][] board = { + {5, 3, 0, 0, 7, 0, 0, 0, 0}, + {6, 0, 0, 1, 9, 5, 0, 0, 0}, + {0, 9, 8, 0, 0, 0, 0, 6, 0}, + {8, 0, 0, 0, 6, 0, 0, 0, 3}, + {4, 0, 0, 8, 0, 3, 0, 0, 1}, + {7, 0, 0, 0, 2, 0, 0, 0, 6}, + {0, 6, 0, 0, 0, 0, 2, 8, 0}, + {0, 0, 0, 4, 1, 9, 0, 0, 5}, + {0, 0, 0, 0, 8, 0, 0, 7, 9} + }; - assertTrue(SudokuSolver.solveSudoku(board), - "Sudoku should be solvable"); - assertBoardValid(board); - } - - @Test - public void testUnsolvableSudoku() { - int[][] board = { - {5, 3, 5, 0, 7, 0, 0, 0, 0}, // Row has duplicate 5s - {6, 0, 0, 1, 9, 5, 0, 0, 0}, - {0, 9, 8, 0, 0, 0, 0, 6, 0}, - {8, 0, 0, 0, 6, 0, 0, 0, 3}, - {4, 0, 0, 8, 0, 3, 0, 0, 1}, - {7, 0, 0, 0, 2, 0, 0, 0, 6}, - {0, 6, 0, 0, 0, 0, 2, 8, 0}, - {0, 0, 0, 4, 1, 9, 0, 0, 5}, - {0, 0, 0, 0, 8, 0, 0, 7, 9} - }; + assertTrue(SudokuSolver.solveSudoku(board)); + assertBoardValid(board); + } - assertFalse(SudokuSolver.solveSudoku(board), - "Sudoku should not be solvable"); - } + @Test + public void testUnsolvableSudoku() { + int[][] board = { + {5, 3, 5, 0, 7, 0, 0, 0, 0}, // duplicate 5 -> unsolvable + {6, 0, 0, 1, 9, 5, 0, 0, 0}, + {0, 9, 8, 0, 0, 0, 0, 6, 0}, + {8, 0, 0, 0, 6, 0, 0, 0, 3}, + {4, 0, 0, 8, 0, 3, 0, 0, 1}, + {7, 0, 0, 0, 2, 0, 0, 0, 6}, + {0, 6, 0, 0, 0, 0, 2, 8, 0}, + {0, 0, 0, 4, 1, 9, 0, 0, 5}, + {0, 0, 0, 0, 8, 0, 0, 7, 9} + }; - @Test - public void testCompleteBoard() { - int[][] board = { - {5, 3, 4, 6, 7, 8, 9, 1, 2}, - {6, 7, 2, 1, 9, 5, 3, 4, 8}, - {1, 9, 8, 3, 4, 2, 5, 6, 7}, - {8, 5, 9, 7, 6, 1, 4, 2, 3}, - {4, 2, 6, 8, 5, 3, 7, 9, 1}, - {7, 1, 3, 9, 2, 4, 8, 5, 6}, - {9, 6, 1, 5, 3, 7, 2, 8, 4}, - {2, 8, 7, 4, 1, 9, 6, 3, 5}, - {3, 4, 5, 2, 8, 6, 1, 7, 9} - }; + assertFalse(SudokuSolver.solveSudoku(board)); + } - assertTrue(SudokuSolver.solveSudoku(board), - "Already solved Sudoku should return true"); - assertBoardValid(board); - } + @Test + public void testAlreadySolvedBoard() { + int[][] board = { + {5, 3, 4, 6, 7, 8, 9, 1, 2}, + {6, 7, 2, 1, 9, 5, 3, 4, 8}, + {1, 9, 8, 3, 4, 2, 5, 6, 7}, + {8, 5, 9, 7, 6, 1, 4, 2, 3}, + {4, 2, 6, 8, 5, 3, 7, 9, 1}, + {7, 1, 3, 9, 2, 4, 8, 5, 6}, + {9, 6, 1, 5, 3, 7, 2, 8, 4}, + {2, 8, 7, 4, 1, 9, 6, 3, 5}, + {3, 4, 5, 2, 8, 6, 1, 7, 9} + }; - /** - * Helper method to validate that the board is correctly solved. - * - * @param board the solved Sudoku board - */ - private void assertBoardValid(int[][] board) { - // Check rows - for (int row = 0; row < 9; row++) { - boolean[] seen = new boolean[10]; - for (int col = 0; col < 9; col++) { - int num = board[row][col]; - assertTrue(num >= 1 && num <= 9, "Invalid number in board"); - assertFalse(seen[num], "Duplicate in row " + row); - seen[num] = true; - } + assertTrue(SudokuSolver.solveSudoku(board)); + assertBoardValid(board); } - // Check columns - for (int col = 0; col < 9; col++) { - boolean[] seen = new boolean[10]; - for (int row = 0; row < 9; row++) { - int num = board[row][col]; - assertFalse(seen[num], "Duplicate in column " + col); - seen[num] = true; - } - } + private void assertBoardValid(int[][] board) { + // check rows + for (int row = 0; row < 9; row++) { + boolean[] seen = new boolean[10]; + for (int col = 0; col < 9; col++) { + int num = board[row][col]; + assertTrue(num >= 1 && num <= 9); + assertFalse(seen[num]); + seen[num] = true; + } + } + + // check columns + for (int col = 0; col < 9; col++) { + boolean[] seen = new boolean[10]; + for (int row = 0; row < 9; row++) { + int num = board[row][col]; + assertFalse(seen[num]); + seen[num] = true; + } + } - // Check 3×3 subgrids - for (int boxRow = 0; boxRow < 3; boxRow++) { - for (int boxCol = 0; boxCol < 3; boxCol++) { - boolean[] seen = new boolean[10]; - for (int i = boxRow * 3; i < boxRow * 3 + 3; i++) { - for (int j = boxCol * 3; j < boxCol * 3 + 3; j++) { - int num = board[i][j]; - assertFalse(seen[num], "Duplicate in subgrid"); - seen[num] = true; - } + // check subgrids + for (int boxRow = 0; boxRow < 3; boxRow++) { + for (int boxCol = 0; boxCol < 3; boxCol++) { + boolean[] seen = new boolean[10]; + for (int i = boxRow * 3; i < boxRow * 3 + 3; i++) { + for (int j = boxCol * 3; j < boxCol * 3 + 3; j++) { + int num = board[i][j]; + assertFalse(seen[num]); + seen[num] = true; + } + } + } } - } } - } -} \ No newline at end of file +} From 00a3b298bce0032edd49a0152dbd3395e2b2ec76 Mon Sep 17 00:00:00 2001 From: harsha08-2k6 Date: Sat, 22 Nov 2025 11:53:10 +0530 Subject: [PATCH 4/5] Fix formatting using Spotless/Google Java Format --- .../backtracking/SudokuSolver.java | 281 ++++++++++-------- .../backtracking/SudokuSolverTest.java | 166 ++++++----- 2 files changed, 244 insertions(+), 203 deletions(-) diff --git a/src/main/java/com/thealgorithms/backtracking/SudokuSolver.java b/src/main/java/com/thealgorithms/backtracking/SudokuSolver.java index 7b9a74913a95..6562def8e697 100644 --- a/src/main/java/com/thealgorithms/backtracking/SudokuSolver.java +++ b/src/main/java/com/thealgorithms/backtracking/SudokuSolver.java @@ -1,140 +1,175 @@ package com.thealgorithms.backtracking; /** - * Sudoku Solver using Backtracking Algorithm. + * Sudoku Solver using Backtracking Algorithm * - * Solves a 9×9 Sudoku grid by filling empty cells (0) such that every row, - * column, and 3×3 subgrid contains all digits 1–9 exactly once. + * This class solves a partially filled 9×9 Sudoku board by finding a valid + * arrangement where every row, column, and 3×3 subgrid contains digits 1-9 + * exactly once. + * + * Algorithm References: + * - Backtracking: https://en.wikipedia.org/wiki/Backtracking + * - Sudoku: https://en.wikipedia.org/wiki/Sudoku + * + * Time Complexity: O(9^(n*n)) in worst case, where n=9 + * Space Complexity: O(n*n) for recursion stack + * + * @author TheAlgorithms */ public class SudokuSolver { - private static final int SIZE = 9; - private static final int SUBGRID = 3; - private static final int EMPTY = 0; - - /** - * Public API: Validates the initial board and triggers the solver. - * - * @param board 9×9 Sudoku grid - * @return true if solvable - */ - public static boolean solveSudoku(int[][] board) { - if (!isInitialBoardValid(board)) { - return false; - } - return solve(board); - } - - /** - * Solves Sudoku recursively using backtracking. - */ - private static boolean solve(int[][] board) { - for (int row = 0; row < SIZE; row++) { - for (int col = 0; col < SIZE; col++) { - - if (board[row][col] == EMPTY) { - - for (int num = 1; num <= SIZE; num++) { - - if (isValid(board, row, col, num)) { - board[row][col] = num; - - if (solve(board)) { - return true; - } - - board[row][col] = EMPTY; - } - } - - return false; - } - } - } - return true; - } - - /** - * Validates initial puzzle (checks duplicates in rows/cols/subgrids). - */ - public static boolean isInitialBoardValid(int[][] board) { - for (int row = 0; row < SIZE; row++) { - for (int col = 0; col < SIZE; col++) { - - int num = board[row][col]; - - if (num != EMPTY) { - board[row][col] = EMPTY; - - if (!isValid(board, row, col, num)) { - board[row][col] = num; - return false; - } - - board[row][col] = num; - } + private static final int SIZE = 9; + private static final int SUBGRID_SIZE = 3; + private static final int EMPTY = 0; + + /** + * Solves the given Sudoku board using backtracking. Modifies the board + * in-place. + * + * @param board 9×9 2D array representing the Sudoku board (0 = empty cell) + * @return true if a valid solution exists, false otherwise + */ + public static boolean solveSudoku(int[][] board) { + for (int row = 0; row < SIZE; row++) { + for (int col = 0; col < SIZE; col++) { + if (board[row][col] == EMPTY) { + for (int num = 1; num <= SIZE; num++) { + if (isValid(board, row, col, num)) { + board[row][col] = num; + + if (solveSudoku(board)) { + return true; + } + + board[row][col] = EMPTY; } + } + return false; } - return true; + } } - - private static boolean isValid(int[][] board, int row, int col, int num) { - return isRowOk(board, row, num) - && isColOk(board, col, num) - && isSubgridOk(board, row, col, num); + return true; + } + + /** + * Checks if placing a number at a given position is valid. + * + * @param board the Sudoku board + * @param row row index + * @param col column index + * @param num number to place (1-9) + * @return true if placement is valid + */ + private static boolean isValid(int[][] board, int row, int col, int num) { + return isRowValid(board, row, num) + && isColumnValid(board, col, num) + && isSubgridValid(board, row, col, num); + } + + /** + * Checks if a number already exists in the given row. + * + * @param board the Sudoku board + * @param row row index + * @param num number to check + * @return true if number is not in row + */ + private static boolean isRowValid(int[][] board, int row, int num) { + for (int col = 0; col < SIZE; col++) { + if (board[row][col] == num) { + return false; + } } - - private static boolean isRowOk(int[][] board, int row, int num) { - for (int col = 0; col < SIZE; col++) { - if (board[row][col] == num) { - return false; - } - } - return true; + return true; + } + + /** + * Checks if a number already exists in the given column. + * + * @param board the Sudoku board + * @param col column index + * @param num number to check + * @return true if number is not in column + */ + private static boolean isColumnValid(int[][] board, int col, int num) { + for (int row = 0; row < SIZE; row++) { + if (board[row][col] == num) { + return false; + } } - - private static boolean isColOk(int[][] board, int col, int num) { - for (int row = 0; row < SIZE; row++) { - if (board[row][col] == num) { - return false; - } + return true; + } + + /** + * Checks if a number already exists in the 3×3 subgrid. + * + * @param board the Sudoku board + * @param row row index + * @param col column index + * @param num number to check + * @return true if number is not in subgrid + */ + private static boolean isSubgridValid( + int[][] board, int row, int col, int num) { + int subgridRow = row - row % SUBGRID_SIZE; + int subgridCol = col - col % SUBGRID_SIZE; + + for (int i = subgridRow; i < subgridRow + SUBGRID_SIZE; i++) { + for (int j = subgridCol; j < subgridCol + SUBGRID_SIZE; j++) { + if (board[i][j] == num) { + return false; } - return true; + } } - - private static boolean isSubgridOk(int[][] board, int row, int col, int num) { - int startRow = row - (row % SUBGRID); - int startCol = col - (col % SUBGRID); - - for (int i = startRow; i < startRow + SUBGRID; i++) { - for (int j = startCol; j < startCol + SUBGRID; j++) { - if (board[i][j] == num) { - return false; - } - } + return true; + } + + /** + * Prints the Sudoku board in a readable format. + * + * @param board the Sudoku board to print + */ + public static void printBoard(int[][] board) { + for (int row = 0; row < SIZE; row++) { + if (row % SUBGRID_SIZE == 0 && row != 0) { + System.out.println("-----------"); + } + for (int col = 0; col < SIZE; col++) { + if (col % SUBGRID_SIZE == 0 && col != 0) { + System.out.print("|"); } - return true; + System.out.print(board[row][col]); + } + System.out.println(); } - - /** - * Utility: Print Sudoku grid. - */ - public static void printBoard(int[][] board) { - for (int row = 0; row < SIZE; row++) { - - if (row % SUBGRID == 0 && row != 0) { - System.out.println("-----------"); - } - - for (int col = 0; col < SIZE; col++) { - - if (col % SUBGRID == 0 && col != 0) { - System.out.print("|"); - } - - System.out.print(board[row][col]); - } - System.out.println(); - } + } + + /** + * Main method demonstrating Sudoku solver functionality. + * + * @param args command line arguments (not used) + */ + public static void main(String[] args) { + int[][] board = { + {5, 3, 0, 0, 7, 0, 0, 0, 0}, + {6, 0, 0, 1, 9, 5, 0, 0, 0}, + {0, 9, 8, 0, 0, 0, 0, 6, 0}, + {8, 0, 0, 0, 6, 0, 0, 0, 3}, + {4, 0, 0, 8, 0, 3, 0, 0, 1}, + {7, 0, 0, 0, 2, 0, 0, 0, 6}, + {0, 6, 0, 0, 0, 0, 2, 8, 0}, + {0, 0, 0, 4, 1, 9, 0, 0, 5}, + {0, 0, 0, 0, 8, 0, 0, 7, 9} + }; + + System.out.println("Sudoku Puzzle:"); + printBoard(board); + + if (solveSudoku(board)) { + System.out.println("\nSolved Sudoku:"); + printBoard(board); + } else { + System.out.println("\nNo solution exists for this Sudoku puzzle."); } -} + } +} \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/backtracking/SudokuSolverTest.java b/src/test/java/com/thealgorithms/backtracking/SudokuSolverTest.java index 1cfb1ffa06d7..126a4c99e280 100644 --- a/src/test/java/com/thealgorithms/backtracking/SudokuSolverTest.java +++ b/src/test/java/com/thealgorithms/backtracking/SudokuSolverTest.java @@ -6,93 +6,99 @@ public class SudokuSolverTest { - @Test - public void testSolvableSudoku() { - int[][] board = { - {5, 3, 0, 0, 7, 0, 0, 0, 0}, - {6, 0, 0, 1, 9, 5, 0, 0, 0}, - {0, 9, 8, 0, 0, 0, 0, 6, 0}, - {8, 0, 0, 0, 6, 0, 0, 0, 3}, - {4, 0, 0, 8, 0, 3, 0, 0, 1}, - {7, 0, 0, 0, 2, 0, 0, 0, 6}, - {0, 6, 0, 0, 0, 0, 2, 8, 0}, - {0, 0, 0, 4, 1, 9, 0, 0, 5}, - {0, 0, 0, 0, 8, 0, 0, 7, 9} - }; + @Test + public void testSolvableSudoku() { + int[][] board = { + {5, 3, 0, 0, 7, 0, 0, 0, 0}, + {6, 0, 0, 1, 9, 5, 0, 0, 0}, + {0, 9, 8, 0, 0, 0, 0, 6, 0}, + {8, 0, 0, 0, 6, 0, 0, 0, 3}, + {4, 0, 0, 8, 0, 3, 0, 0, 1}, + {7, 0, 0, 0, 2, 0, 0, 0, 6}, + {0, 6, 0, 0, 0, 0, 2, 8, 0}, + {0, 0, 0, 4, 1, 9, 0, 0, 5}, + {0, 0, 0, 0, 8, 0, 0, 7, 9} + }; - assertTrue(SudokuSolver.solveSudoku(board)); - assertBoardValid(board); - } + assertTrue(SudokuSolver.solveSudoku(board), "Sudoku should be solvable"); + assertBoardValid(board); + } - @Test - public void testUnsolvableSudoku() { - int[][] board = { - {5, 3, 5, 0, 7, 0, 0, 0, 0}, // duplicate 5 -> unsolvable - {6, 0, 0, 1, 9, 5, 0, 0, 0}, - {0, 9, 8, 0, 0, 0, 0, 6, 0}, - {8, 0, 0, 0, 6, 0, 0, 0, 3}, - {4, 0, 0, 8, 0, 3, 0, 0, 1}, - {7, 0, 0, 0, 2, 0, 0, 0, 6}, - {0, 6, 0, 0, 0, 0, 2, 8, 0}, - {0, 0, 0, 4, 1, 9, 0, 0, 5}, - {0, 0, 0, 0, 8, 0, 0, 7, 9} - }; + @Test + public void testUnsolvableSudoku() { + int[][] board = { + {5, 3, 5, 0, 7, 0, 0, 0, 0}, + {6, 0, 0, 1, 9, 5, 0, 0, 0}, + {0, 9, 8, 0, 0, 0, 0, 6, 0}, + {8, 0, 0, 0, 6, 0, 0, 0, 3}, + {4, 0, 0, 8, 0, 3, 0, 0, 1}, + {7, 0, 0, 0, 2, 0, 0, 0, 6}, + {0, 6, 0, 0, 0, 0, 2, 8, 0}, + {0, 0, 0, 4, 1, 9, 0, 0, 5}, + {0, 0, 0, 0, 8, 0, 0, 7, 9} + }; - assertFalse(SudokuSolver.solveSudoku(board)); - } + assertFalse(SudokuSolver.solveSudoku(board), "Sudoku should not be solvable"); + } - @Test - public void testAlreadySolvedBoard() { - int[][] board = { - {5, 3, 4, 6, 7, 8, 9, 1, 2}, - {6, 7, 2, 1, 9, 5, 3, 4, 8}, - {1, 9, 8, 3, 4, 2, 5, 6, 7}, - {8, 5, 9, 7, 6, 1, 4, 2, 3}, - {4, 2, 6, 8, 5, 3, 7, 9, 1}, - {7, 1, 3, 9, 2, 4, 8, 5, 6}, - {9, 6, 1, 5, 3, 7, 2, 8, 4}, - {2, 8, 7, 4, 1, 9, 6, 3, 5}, - {3, 4, 5, 2, 8, 6, 1, 7, 9} - }; + @Test + public void testCompleteBoard() { + int[][] board = { + {5, 3, 4, 6, 7, 8, 9, 1, 2}, + {6, 7, 2, 1, 9, 5, 3, 4, 8}, + {1, 9, 8, 3, 4, 2, 5, 6, 7}, + {8, 5, 9, 7, 6, 1, 4, 2, 3}, + {4, 2, 6, 8, 5, 3, 7, 9, 1}, + {7, 1, 3, 9, 2, 4, 8, 5, 6}, + {9, 6, 1, 5, 3, 7, 2, 8, 4}, + {2, 8, 7, 4, 1, 9, 6, 3, 5}, + {3, 4, 5, 2, 8, 6, 1, 7, 9} + }; - assertTrue(SudokuSolver.solveSudoku(board)); - assertBoardValid(board); - } + assertTrue(SudokuSolver.solveSudoku(board), + "Already solved Sudoku should return true"); + assertBoardValid(board); + } - private void assertBoardValid(int[][] board) { - // check rows - for (int row = 0; row < 9; row++) { - boolean[] seen = new boolean[10]; - for (int col = 0; col < 9; col++) { - int num = board[row][col]; - assertTrue(num >= 1 && num <= 9); - assertFalse(seen[num]); - seen[num] = true; - } - } + /** + * Helper method to validate that the board is correctly solved. + * + * @param board the solved Sudoku board + */ + private void assertBoardValid(int[][] board) { + // Check rows + for (int row = 0; row < 9; row++) { + boolean[] seen = new boolean[10]; + for (int col = 0; col < 9; col++) { + int num = board[row][col]; + assertTrue(num >= 1 && num <= 9, "Invalid number in board"); + assertFalse(seen[num], "Duplicate in row " + row); + seen[num] = true; + } + } - // check columns - for (int col = 0; col < 9; col++) { - boolean[] seen = new boolean[10]; - for (int row = 0; row < 9; row++) { - int num = board[row][col]; - assertFalse(seen[num]); - seen[num] = true; - } - } + // Check columns + for (int col = 0; col < 9; col++) { + boolean[] seen = new boolean[10]; + for (int row = 0; row < 9; row++) { + int num = board[row][col]; + assertFalse(seen[num], "Duplicate in column " + col); + seen[num] = true; + } + } - // check subgrids - for (int boxRow = 0; boxRow < 3; boxRow++) { - for (int boxCol = 0; boxCol < 3; boxCol++) { - boolean[] seen = new boolean[10]; - for (int i = boxRow * 3; i < boxRow * 3 + 3; i++) { - for (int j = boxCol * 3; j < boxCol * 3 + 3; j++) { - int num = board[i][j]; - assertFalse(seen[num]); - seen[num] = true; - } - } - } + // Check 3×3 subgrids + for (int boxRow = 0; boxRow < 3; boxRow++) { + for (int boxCol = 0; boxCol < 3; boxCol++) { + boolean[] seen = new boolean[10]; + for (int i = boxRow * 3; i < boxRow * 3 + 3; i++) { + for (int j = boxCol * 3; j < boxCol * 3 + 3; j++) { + int num = board[i][j]; + assertFalse(seen[num], "Duplicate in subgrid"); + seen[num] = true; + } } + } } -} + } +} \ No newline at end of file From 3169178c1f5bcbec0e5962516d75b6166a24e5cb Mon Sep 17 00:00:00 2001 From: harsha08-2k6 Date: Sat, 22 Nov 2025 12:00:56 +0530 Subject: [PATCH 5/5] =?UTF-8?q?Add=20Sudoku=20Solver=20using=20Backtrackin?= =?UTF-8?q?g=20algorithm=20-=20Implements=20depth-first=20backtracking=20t?= =?UTF-8?q?o=20solve=209=C3=979=20Sudoku=20puzzles=20-=20Includes=20valida?= =?UTF-8?q?tion=20for=20rows,=20columns,=20and=203=C3=973=20subgrids=20-?= =?UTF-8?q?=20Provides=20clean,=20modular=20implementation=20with=20helper?= =?UTF-8?q?=20methods=20-=20Includes=20comprehensive=20unit=20tests=20for?= =?UTF-8?q?=20solvable=20and=20unsolvable=20cases=20-=20Time=20Complexity:?= =?UTF-8?q?=20O(9^(n=C2=B2))=20in=20worst=20case=20-=20Space=20Complexity:?= =?UTF-8?q?=20O(n=C2=B2)=20for=20recursion=20stack?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/thealgorithms/backtracking/SudokuSolverTest.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/thealgorithms/backtracking/SudokuSolverTest.java b/src/test/java/com/thealgorithms/backtracking/SudokuSolverTest.java index 126a4c99e280..ea08391d2ade 100644 --- a/src/test/java/com/thealgorithms/backtracking/SudokuSolverTest.java +++ b/src/test/java/com/thealgorithms/backtracking/SudokuSolverTest.java @@ -20,7 +20,8 @@ public void testSolvableSudoku() { {0, 0, 0, 0, 8, 0, 0, 7, 9} }; - assertTrue(SudokuSolver.solveSudoku(board), "Sudoku should be solvable"); + assertTrue(SudokuSolver.solveSudoku(board), + "Sudoku should be solvable"); assertBoardValid(board); } @@ -38,7 +39,8 @@ public void testUnsolvableSudoku() { {0, 0, 0, 0, 8, 0, 0, 7, 9} }; - assertFalse(SudokuSolver.solveSudoku(board), "Sudoku should not be solvable"); + assertFalse(SudokuSolver.solveSudoku(board), + "Sudoku should not be solvable"); } @Test @@ -56,7 +58,7 @@ public void testCompleteBoard() { }; assertTrue(SudokuSolver.solveSudoku(board), - "Already solved Sudoku should return true"); + "Already solved Sudoku should return true"); assertBoardValid(board); }