Skip to content

Commit f8b35c0

Browse files
author
khalidx3
committed
Fixed Clang Format issues
1 parent 66a771b commit f8b35c0

File tree

1 file changed

+69
-69
lines changed

1 file changed

+69
-69
lines changed

src/main/java/com/thealgorithms/backtracking/SudokuSolver.java

Lines changed: 69 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -9,86 +9,86 @@
99
*/
1010
public final class SudokuSolver {
1111

12-
private SudokuSolver() {
13-
} // Prevent instantiation
12+
private SudokuSolver() {
13+
} // Prevent instantiation
1414

15-
/**
16-
* Solves the Sudoku puzzle using backtracking.
17-
*
18-
* @param sudoku 9x9 Sudoku grid, empty cells are 0
19-
* @param row current row
20-
* @param col current column
21-
* @return true if solved, false otherwise
22-
*/
23-
public static boolean sudokuSolver(int[][] sudoku, int row, int col) {
24-
if (row == 9) {
25-
return true;
26-
}
15+
/**
16+
* Solves the Sudoku puzzle using backtracking.
17+
*
18+
* @param sudoku 9x9 Sudoku grid, empty cells are 0
19+
* @param row current row
20+
* @param col current column
21+
* @return true if solved, false otherwise
22+
*/
23+
public static boolean sudokuSolver(int[][] sudoku, int row, int col) {
24+
if (row == 9) {
25+
return true;
26+
}
2727

28-
int nextRow = row;
29-
int nextCol = col + 1;
30-
if (nextCol == 9) {
31-
nextRow = row + 1;
32-
nextCol = 0;
33-
}
28+
int nextRow = row;
29+
int nextCol = col + 1;
30+
if (nextCol == 9) {
31+
nextRow = row + 1;
32+
nextCol = 0;
33+
}
3434

35-
if (sudoku[row][col] != 0) {
36-
return sudokuSolver(sudoku, nextRow, nextCol);
37-
}
35+
if (sudoku[row][col] != 0) {
36+
return sudokuSolver(sudoku, nextRow, nextCol);
37+
}
3838

39-
for (int num = 1; num <= 9; num++) {
40-
if (isSafe(sudoku, row, col, num)) {
41-
sudoku[row][col] = num;
42-
if (sudokuSolver(sudoku, nextRow, nextCol)) {
43-
return true;
44-
}
45-
sudoku[row][col] = 0; // backtrack
46-
}
39+
for (int num = 1; num <= 9; num++) {
40+
if (isSafe(sudoku, row, col, num)) {
41+
sudoku[row][col] = num;
42+
if (sudokuSolver(sudoku, nextRow, nextCol)) {
43+
return true;
44+
}
45+
sudoku[row][col] = 0; // backtrack
4746
}
47+
}
4848

49-
return false;
50-
}
49+
return false;
50+
}
5151

52-
/**
53-
* Checks if placing num at (row, col) is safe.
54-
*
55-
* @param sudoku 9x9 Sudoku grid
56-
* @param row current row
57-
* @param col current column
58-
* @param num number to place
59-
* @return true if safe, false otherwise
60-
*/
61-
private static boolean isSafe(int[][] sudoku, int row, int col, int num) {
62-
for (int i = 0; i < 9; i++) {
63-
if (sudoku[row][i] == num || sudoku[i][col] == num) {
64-
return false;
65-
}
52+
/**
53+
* Checks if placing num at (row, col) is safe.
54+
*
55+
* @param sudoku 9x9 Sudoku grid
56+
* @param row current row
57+
* @param col current column
58+
* @param num number to place
59+
* @return true if safe, false otherwise
60+
*/
61+
private static boolean isSafe(int[][] sudoku, int row, int col, int num) {
62+
for (int i = 0; i < 9; i++) {
63+
if (sudoku[row][i] == num || sudoku[i][col] == num) {
64+
return false;
6665
}
66+
}
6767

68-
int startRow = row - row % 3;
69-
int startCol = col - col % 3;
70-
for (int i = startRow; i < startRow + 3; i++) {
71-
for (int j = startCol; j < startCol + 3; j++) {
72-
if (sudoku[i][j] == num) {
73-
return false;
74-
}
75-
}
68+
int startRow = row - row % 3;
69+
int startCol = col - col % 3;
70+
for (int i = startRow; i < startRow + 3; i++) {
71+
for (int j = startCol; j < startCol + 3; j++) {
72+
if (sudoku[i][j] == num) {
73+
return false;
74+
}
7675
}
76+
}
7777

78-
return true;
79-
}
78+
return true;
79+
}
8080

81-
/**
82-
* Prints the Sudoku grid.
83-
*
84-
* @param sudoku 9x9 Sudoku grid
85-
*/
86-
public static void printSudoku(int[][] sudoku) {
87-
for (int i = 0; i < 9; i++) {
88-
for (int j = 0; j < 9; j++) {
89-
System.out.print(sudoku[i][j] + " ");
90-
}
91-
System.out.println();
81+
/**
82+
* Prints the Sudoku grid.
83+
*
84+
* @param sudoku 9x9 Sudoku grid
85+
*/
86+
public static void printSudoku(int[][] sudoku) {
87+
for (int i = 0; i < 9; i++) {
88+
for (int j = 0; j < 9; j++) {
89+
System.out.print(sudoku[i][j] + " ");
9290
}
93-
}
91+
System.out.println();
92+
}
93+
}
9494
}

0 commit comments

Comments
 (0)