1
1
package com .thealgorithms .others ;
2
2
3
3
/**
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.
7
9
*/
8
10
final class Sudoku {
9
11
@@ -16,6 +18,7 @@ private Sudoku() {
16
18
* - It should not be present in the same row.
17
19
* - It should not be present in the same column.
18
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).
19
22
*
20
23
* @param board The current state of the Sudoku board.
21
24
* @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) {
56
59
57
60
/**
58
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).
59
65
* The algorithm finds an empty cell and tries placing numbers from 1 to 9.
60
66
* The standard version of Sudoku uses numbers from 1 to 9, so the algorithm can be
61
67
* 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.
65
72
*
66
73
* @param board The current state of the Sudoku board.
67
74
* @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) {
92
99
return true ;
93
100
}
94
101
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.
96
104
for (int num = 1 ; num <= n ; num ++) {
97
105
if (isSafe (board , row , col , num )) {
98
106
board [row ][col ] = num ;
@@ -116,6 +124,8 @@ public static boolean solveSudoku(int[][] board, int n) {
116
124
*/
117
125
public static void print (int [][] board , int n ) {
118
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
119
129
for (int r = 0 ; r < n ; r ++) {
120
130
for (int d = 0 ; d < n ; d ++) {
121
131
System .out .print (board [r ][d ]);
0 commit comments