Skip to content

Commit 4c80e7f

Browse files
committed
Add changes suggested
1 parent e164a83 commit 4c80e7f

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.thealgorithms.others;
22

33
/**
4-
* A class that provides methods to solve a 9x9 Sudoku puzzle using a backtracking approach.
5-
* The Sudoku board is represented as a 2D array, and the methods are designed to
6-
* check for safe placements of numbers, solve the puzzle recursively, and print the board.
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.
79
*/
810
final class Sudoku {
911

@@ -16,6 +18,7 @@ private Sudoku() {
1618
* - It should not be present in the same row.
1719
* - It should not be present in the same column.
1820
* - 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).
1922
*
2023
* @param board The current state of the Sudoku board.
2124
* @param row The row index where the number is to be placed.
@@ -56,12 +59,16 @@ public static boolean isSafe(int[][] board, int row, int col, int num) {
5659

5760
/**
5861
* 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).
5965
* The algorithm finds an empty cell and tries placing numbers from 1 to 9.
6066
* The standard version of Sudoku uses numbers from 1 to 9, so the algorithm can be
6167
* easily modified for other variations of the game.
62-
* If a number placement is valid (checked via `isSafe`), the number is placed and the function
63-
* recursively attempts to solve the rest of the puzzle.
64-
* If no solution is possible, the number is removed (backtracked), and the process is repeated.
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.
6572
*
6673
* @param board The current state of the Sudoku board.
6774
* @param n The size of the Sudoku board (typically 9 for a standard puzzle).
@@ -92,7 +99,8 @@ public static boolean solveSudoku(int[][] board, int n) {
9299
return true;
93100
}
94101

95-
// Try placing numbers 1 to n in the empty cell (typically n=9)
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.
96104
for (int num = 1; num <= n; num++) {
97105
if (isSafe(board, row, col, num)) {
98106
board[row][col] = num;
@@ -116,6 +124,8 @@ public static boolean solveSudoku(int[][] board, int n) {
116124
*/
117125
public static void print(int[][] board, int n) {
118126
// 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
119129
for (int r = 0; r < n; r++) {
120130
for (int d = 0; d < n; d++) {
121131
System.out.print(board[r][d]);

0 commit comments

Comments
 (0)