Skip to content

Commit 66ee59c

Browse files
authored
Add function documentation in Sudoku.java (#5532)
* Add function documentation and parameterized tests to Sudoku.java * Update directory * Update directory * Fix clang format errors * Change * Fix * Fix * Fix * Fix * Fix * Fix * Fix * Remove extra line * Change values * Fix * Remove test * Update directory * Small comment fix * Add comment * Generalize comment * Fix comment * Update directory * Fix comment * Add changes suggested --------- Co-authored-by: Hardvan <[email protected]>
1 parent be8df21 commit 66ee59c

File tree

2 files changed

+65
-20
lines changed

2 files changed

+65
-20
lines changed

DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@
238238
* [KnapsackMemoization](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/KnapsackMemoization.java)
239239
* [LevenshteinDistance](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LevenshteinDistance.java)
240240
* [LongestAlternatingSubsequence](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequence.java)
241+
* [LongestArithmeticSubsequence](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java)
241242
* [LongestCommonSubsequence](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LongestCommonSubsequence.java)
242243
* [LongestIncreasingSubsequence](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LongestIncreasingSubsequence.java)
243244
* [LongestPalindromicSubsequence](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubsequence.java)
@@ -733,6 +734,7 @@
733734
* [KnapsackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/KnapsackTest.java)
734735
* [LevenshteinDistanceTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LevenshteinDistanceTests.java)
735736
* [LongestAlternatingSubsequenceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequenceTest.java)
737+
* [LongestArithmeticSubsequenceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java)
736738
* [LongestIncreasingSubsequenceTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestIncreasingSubsequenceTests.java)
737739
* [LongestPalindromicSubstringTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubstringTest.java)
738740
* [LongestValidParenthesesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestValidParenthesesTest.java)

src/main/java/com/thealgorithms/others/Sudoku.java

+63-20
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,47 @@
11
package com.thealgorithms.others;
22

3+
/**
4+
* A class that provides methods to solve Sudoku puzzles of any n x n size
5+
* using a backtracking approach, where n must be a perfect square.
6+
* The algorithm checks for safe number placements in rows, columns,
7+
* and subgrids (which are sqrt(n) x sqrt(n) in size) and recursively solves the puzzle.
8+
* Though commonly used for 9x9 grids, it is adaptable to other valid Sudoku dimensions.
9+
*/
310
final class Sudoku {
11+
412
private Sudoku() {
513
}
614

15+
/**
16+
* Checks if placing a number in a specific position on the Sudoku board is safe.
17+
* The number is considered safe if it does not violate any of the Sudoku rules:
18+
* - It should not be present in the same row.
19+
* - It should not be present in the same column.
20+
* - It should not be present in the corresponding 3x3 subgrid.
21+
* - It should not be present in the corresponding subgrid, which is sqrt(n) x sqrt(n) in size (e.g., for a 9x9 grid, the subgrid will be 3x3).
22+
*
23+
* @param board The current state of the Sudoku board.
24+
* @param row The row index where the number is to be placed.
25+
* @param col The column index where the number is to be placed.
26+
* @param num The number to be placed on the board.
27+
* @return True if the placement is safe, otherwise false.
28+
*/
729
public static boolean isSafe(int[][] board, int row, int col, int num) {
8-
// Row has the unique (row-clash)
30+
// Check the row for duplicates
931
for (int d = 0; d < board.length; d++) {
10-
// Check if the number we are trying to
11-
// place is already present in
12-
// that row, return false;
1332
if (board[row][d] == num) {
1433
return false;
1534
}
1635
}
1736

18-
// Column has the unique numbers (column-clash)
37+
// Check the column for duplicates
1938
for (int r = 0; r < board.length; r++) {
20-
// Check if the number
21-
// we are trying to
22-
// place is already present in
23-
// that column, return false;
2439
if (board[r][col] == num) {
2540
return false;
2641
}
2742
}
2843

29-
// Corresponding square has
30-
// unique number (box-clash)
44+
// Check the corresponding 3x3 subgrid for duplicates
3145
int sqrt = (int) Math.sqrt(board.length);
3246
int boxRowStart = row - row % sqrt;
3347
int boxColStart = col - col % sqrt;
@@ -40,22 +54,37 @@ public static boolean isSafe(int[][] board, int row, int col, int num) {
4054
}
4155
}
4256

43-
// if there is no clash, it's safe
4457
return true;
4558
}
4659

60+
/**
61+
* Solves the Sudoku puzzle using backtracking.
62+
* The algorithm finds an empty cell and tries placing numbers
63+
* from 1 to n, where n is the size of the board
64+
* (for example, from 1 to 9 in a standard 9x9 Sudoku).
65+
* The algorithm finds an empty cell and tries placing numbers from 1 to 9.
66+
* The standard version of Sudoku uses numbers from 1 to 9, so the algorithm can be
67+
* easily modified for other variations of the game.
68+
* If a number placement is valid (checked via `isSafe`), the number is
69+
* placed and the function recursively attempts to solve the rest of the puzzle.
70+
* If no solution is possible, the number is removed (backtracked),
71+
* and the process is repeated.
72+
*
73+
* @param board The current state of the Sudoku board.
74+
* @param n The size of the Sudoku board (typically 9 for a standard puzzle).
75+
* @return True if the Sudoku puzzle is solvable, false otherwise.
76+
*/
4777
public static boolean solveSudoku(int[][] board, int n) {
4878
int row = -1;
4979
int col = -1;
5080
boolean isEmpty = true;
81+
82+
// Find the next empty cell
5183
for (int i = 0; i < n; i++) {
5284
for (int j = 0; j < n; j++) {
5385
if (board[i][j] == 0) {
5486
row = i;
5587
col = j;
56-
57-
// We still have some remaining
58-
// missing values in Sudoku
5988
isEmpty = false;
6089
break;
6190
}
@@ -70,12 +99,12 @@ public static boolean solveSudoku(int[][] board, int n) {
7099
return true;
71100
}
72101

73-
// Else for each-row backtrack
102+
// Try placing numbers 1 to n in the empty cell (n should be a perfect square)
103+
// Eg: n=9 for a standard 9x9 Sudoku puzzle, n=16 for a 16x16 puzzle, etc.
74104
for (int num = 1; num <= n; num++) {
75105
if (isSafe(board, row, col, num)) {
76106
board[row][col] = num;
77107
if (solveSudoku(board, n)) {
78-
// print(board, n);
79108
return true;
80109
} else {
81110
// replace it
@@ -86,8 +115,17 @@ public static boolean solveSudoku(int[][] board, int n) {
86115
return false;
87116
}
88117

118+
/**
119+
* Prints the current state of the Sudoku board in a readable format.
120+
* Each row is printed on a new line, with numbers separated by spaces.
121+
*
122+
* @param board The current state of the Sudoku board.
123+
* @param n The size of the Sudoku board (typically 9 for a standard puzzle).
124+
*/
89125
public static void print(int[][] board, int n) {
90-
// We got the answer, just print it
126+
// Print the board in a nxn grid format
127+
// if n=9, print the board in a 9x9 grid format
128+
// if n=16, print the board in a 16x16 grid format
91129
for (int r = 0; r < n; r++) {
92130
for (int d = 0; d < n; d++) {
93131
System.out.print(board[r][d]);
@@ -101,7 +139,13 @@ public static void print(int[][] board, int n) {
101139
}
102140
}
103141

104-
// Driver Code
142+
/**
143+
* The driver method to demonstrate solving a Sudoku puzzle.
144+
* A sample 9x9 Sudoku puzzle is provided, and the program attempts to solve it
145+
* using the `solveSudoku` method. If a solution is found, it is printed to the console.
146+
*
147+
* @param args Command-line arguments (not used in this program).
148+
*/
105149
public static void main(String[] args) {
106150
int[][] board = new int[][] {
107151
{3, 0, 6, 5, 0, 8, 4, 0, 0},
@@ -117,7 +161,6 @@ public static void main(String[] args) {
117161
int n = board.length;
118162

119163
if (solveSudoku(board, n)) {
120-
// print solution
121164
print(board, n);
122165
} else {
123166
System.out.println("No solution");

0 commit comments

Comments
 (0)