1
1
package com .thealgorithms .backtracking ;
2
2
3
3
/**
4
- * Solves a Sudoku of any level and prints solved Sudoku
4
+ * Solves a Sudoku of any level and prints solved Sudoku.
5
+ * This class is a utility and should not be instantiated.
5
6
* @author Indraneela Doradla (<a href="https://github.com/captiosus1">git-Indraneela Doradla</a>)
6
7
*/
7
8
public final class SudokuSolver {
8
9
10
+ /**
11
+ * Private constructor to prevent instantiation.
12
+ */
9
13
private SudokuSolver () {
10
- // Private constructor to prevent instantiation
14
+ // Utility class
11
15
}
12
16
13
17
/**
14
- * Solves the Sudoku using backtracking
18
+ * Solves the Sudoku using backtracking.
15
19
* @param board the Sudoku grid
16
20
* @return boolean indicating if the Sudoku can be solved
17
21
*/
18
22
public static boolean solveSudoku (int [][] board ) {
19
- int r = -1 , c = -1 ;
23
+ int r = -1 ;
24
+ int c = -1 ;
20
25
boolean isEmpty = true ;
21
26
22
- // Finding the first empty position
27
+ // Find the first empty position
23
28
for (int i = 0 ; i < board .length ; i ++) {
24
29
for (int j = 0 ; j < board [0 ].length ; j ++) {
25
30
if (board [i ][j ] == 0 ) {
@@ -29,7 +34,9 @@ public static boolean solveSudoku(int[][] board) {
29
34
break ;
30
35
}
31
36
}
32
- if (!isEmpty ) break ;
37
+ if (!isEmpty ) {
38
+ break ;
39
+ }
33
40
}
34
41
35
42
// If no empty position is found, the Sudoku is solved
@@ -52,7 +59,7 @@ public static boolean solveSudoku(int[][] board) {
52
59
}
53
60
54
61
/**
55
- * Checks if placing a number at the given position is valid
62
+ * Checks if placing a number at the given position is valid.
56
63
* @param board the Sudoku grid
57
64
* @param r row index
58
65
* @param c column index
@@ -84,7 +91,7 @@ public static boolean isSafe(int[][] board, int r, int c, int val) {
84
91
}
85
92
86
93
/**
87
- * Prints the Sudoku grid
94
+ * Prints the Sudoku grid.
88
95
* @param board the Sudoku grid
89
96
*/
90
97
public static void printSudoku (int [][] board ) {
0 commit comments