11package com .thealgorithms .backtracking ;
22
33/**
4- * Sudoku Solver using Backtracking Algorithm
5- * Solves a 9x9 Sudoku puzzle by filling empty cells with valid digits (1-9)
4+ * Sudoku Solver using Backtracking Algorithm.
5+ * Solves a 9x9 Sudoku puzzle by filling empty cells with valid digits (1-9).
66 *
7- * @author Navadeep0007
7+ * @author prasanth-30011
88 */
99public final class SudokuSolver {
1010
@@ -13,14 +13,14 @@ public final class SudokuSolver {
1313 private static final int EMPTY_CELL = 0 ;
1414
1515 private SudokuSolver () {
16- // Utility class, prevent instantiation
16+ // Prevent instantiation
1717 }
1818
1919 /**
20- * Solves the Sudoku puzzle using backtracking
20+ * Public method to solve a Sudoku puzzle.
2121 *
22- * @param board 9x9 Sudoku board with 0 representing empty cells
23- * @return true if puzzle is solved, false otherwise
22+ * @param board 9x9 Sudoku grid (0 means empty)
23+ * @return true if solved, false otherwise
2424 */
2525 public static boolean solveSudoku (int [][] board ) {
2626 if (board == null || board .length != GRID_SIZE ) {
@@ -37,15 +37,16 @@ public static boolean solveSudoku(int[][] board) {
3737 }
3838
3939 /**
40- * Recursive helper method to solve the Sudoku puzzle
40+ * Recursive helper that applies backtracking.
4141 *
42- * @param board the Sudoku board
43- * @return true if solution is found , false otherwise
42+ * @param board Sudoku board
43+ * @return true if solved , false otherwise
4444 */
4545 private static boolean solve (int [][] board ) {
4646 for (int row = 0 ; row < GRID_SIZE ; row ++) {
4747 for (int col = 0 ; col < GRID_SIZE ; col ++) {
4848 if (board [row ][col ] == EMPTY_CELL ) {
49+
4950 for (int number = 1 ; number <= GRID_SIZE ; number ++) {
5051 if (isValidPlacement (board , row , col , number )) {
5152 board [row ][col ] = number ;
@@ -54,104 +55,36 @@ private static boolean solve(int[][] board) {
5455 return true ;
5556 }
5657
57- // Backtrack
58- board [row ][col ] = EMPTY_CELL ;
58+ board [row ][col ] = EMPTY_CELL ; // Backtrack
5959 }
6060 }
61- return false ;
61+
62+ return false ; // No number fits here
6263 }
6364 }
6465 }
65- return true ;
66+ return true ; // Solved completely
6667 }
6768
68- /**
69- * Checks if placing a number at given position is valid
70- *
71- * @param board the Sudoku board
72- * @param row row index
73- * @param col column index
74- * @param number number to place (1-9)
75- * @return true if placement is valid, false otherwise
76- */
7769 private static boolean isValidPlacement (int [][] board , int row , int col , int number ) {
78- return !isNumberInRow (board , row , number ) && !isNumberInColumn (board , col , number ) && !isNumberInSubgrid (board , row , col , number );
79- }
80-
81- /**
82- * Checks if number exists in the given row
83- *
84- * @param board the Sudoku board
85- * @param row row index
86- * @param number number to check
87- * @return true if number exists in row, false otherwise
88- */
89- private static boolean isNumberInRow (int [][] board , int row , int number ) {
90- for (int col = 0 ; col < GRID_SIZE ; col ++) {
91- if (board [row ][col ] == number ) {
92- return true ;
93- }
94- }
95- return false ;
96- }
97-
98- /**
99- * Checks if number exists in the given column
100- *
101- * @param board the Sudoku board
102- * @param col column index
103- * @param number number to check
104- * @return true if number exists in column, false otherwise
105- */
106- private static boolean isNumberInColumn (int [][] board , int col , int number ) {
107- for (int row = 0 ; row < GRID_SIZE ; row ++) {
108- if (board [row ][col ] == number ) {
109- return true ;
70+ // Row and column check
71+ for (int i = 0 ; i < GRID_SIZE ; i ++) {
72+ if (board [row ][i ] == number || board [i ][col ] == number ) {
73+ return false ;
11074 }
11175 }
112- return false ;
113- }
114-
115- /**
116- * Checks if number exists in the 3x3 subgrid
117- *
118- * @param board the Sudoku board
119- * @param row row index
120- * @param col column index
121- * @param number number to check
122- * @return true if number exists in subgrid, false otherwise
123- */
124- private static boolean isNumberInSubgrid (int [][] board , int row , int col , int number ) {
125- int subgridRowStart = row - row % SUBGRID_SIZE ;
126- int subgridColStart = col - col % SUBGRID_SIZE ;
76+ // Subgrid check
77+ int startRow = (row / SUBGRID_SIZE ) * SUBGRID_SIZE ;
78+ int startCol = (col / SUBGRID_SIZE ) * SUBGRID_SIZE ;
12779
128- for (int i = subgridRowStart ; i < subgridRowStart + SUBGRID_SIZE ; i ++) {
129- for (int j = subgridColStart ; j < subgridColStart + SUBGRID_SIZE ; j ++) {
130- if (board [i ][ j ] == number ) {
131- return true ;
80+ for (int r = 0 ; r < SUBGRID_SIZE ; r ++) {
81+ for (int c = 0 ; c < SUBGRID_SIZE ; c ++) {
82+ if (board [startRow + r ][ startCol + c ] == number ) {
83+ return false ;
13284 }
13385 }
13486 }
135- return false ;
136- }
13787
138- /**
139- * Prints the Sudoku board
140- *
141- * @param board the Sudoku board
142- */
143- public static void printBoard (int [][] board ) {
144- for (int row = 0 ; row < GRID_SIZE ; row ++) {
145- if (row % SUBGRID_SIZE == 0 && row != 0 ) {
146- System .out .println ("-----------" );
147- }
148- for (int col = 0 ; col < GRID_SIZE ; col ++) {
149- if (col % SUBGRID_SIZE == 0 && col != 0 ) {
150- System .out .print ("|" );
151- }
152- System .out .print (board [row ][col ]);
153- }
154- System .out .println ();
155- }
88+ return true ;
15689 }
15790}
0 commit comments