Skip to content

Commit 06d0592

Browse files
author
khalidx3
committed
Fixed formatting and build issues for Sudoku Solver
1 parent f554ae0 commit 06d0592

File tree

1 file changed

+40
-41
lines changed

1 file changed

+40
-41
lines changed
Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,58 @@
11
package main.java.com.thealgorithms.backtracking;
22

33
public class SudokuSolver {
4-
public static boolean sudokuSolver(int sudoku[][], int row, int col) {
5-
// base case
4+
5+
public static boolean sudokuSolver(int[][] sudoku, int row, int col) {
66
if (row == 9) {
7-
return true;
7+
return true; // base case: reached the end
88
}
9-
// recursion
10-
int nextRow = row, nextCol = col + 1;
11-
if (col + 1 == 9) {
9+
10+
int nextRow = row;
11+
int nextCol = col + 1;
12+
if (nextCol == 9) {
1213
nextRow = row + 1;
1314
nextCol = 0;
1415
}
16+
1517
if (sudoku[row][col] != 0) {
1618
return sudokuSolver(sudoku, nextRow, nextCol);
1719
}
18-
for (int digit = 1; digit <= 9; digit++) {
19-
if (isSafe(sudoku, row, col, digit)) {
20-
sudoku[row][col] = digit;
2120

21+
for (int num = 1; num <= 9; num++) {
22+
if (isSafe(sudoku, row, col, num)) {
23+
sudoku[row][col] = num;
2224
if (sudokuSolver(sudoku, nextRow, nextCol)) {
2325
return true;
2426
}
25-
sudoku[row][col] = 0;
27+
sudoku[row][col] = 0; // backtrack
2628
}
2729
}
28-
2930
return false;
3031
}
3132

32-
public static boolean isSafe(int sudoku[][], int row, int col, int digit) {
33-
// col
34-
for (int i = 0; i <= 8; i++) {
35-
if (sudoku[i][col] == digit) {
36-
return false;
37-
}
38-
}
39-
// row
40-
for (int j = 0; j <= 8; j++) {
41-
if (sudoku[row][j] == digit) {
33+
private static boolean isSafe(int[][] sudoku, int row, int col, int num) {
34+
// Check row and column
35+
for (int i = 0; i < 9; i++) {
36+
if (sudoku[row][i] == num || sudoku[i][col] == num) {
4237
return false;
4338
}
4439
}
45-
// grid
46-
int sr = (row / 3) * 3;
47-
int sc = (col / 3) * 3;
48-
for (int i = sr; i < sr + 3; i++) {
49-
for (int j = sc; i < sr + 3; i++) {
50-
if (sudoku[i][j] == digit) {
40+
41+
// Check 3x3 subgrid
42+
int startRow = row - row % 3;
43+
int startCol = col - col % 3;
44+
for (int i = startRow; i < startRow + 3; i++) {
45+
for (int j = startCol; j < startCol + 3; j++) {
46+
if (sudoku[i][j] == num) {
5147
return false;
5248
}
5349
}
5450
}
51+
5552
return true;
5653
}
5754

58-
public static void print(int sudoku[][]) {
55+
private static void printSudoku(int[][] sudoku) {
5956
for (int i = 0; i < 9; i++) {
6057
for (int j = 0; j < 9; j++) {
6158
System.out.print(sudoku[i][j] + " ");
@@ -65,21 +62,23 @@ public static void print(int sudoku[][]) {
6562
}
6663

6764
public static void main(String[] args) {
68-
int sudoku[][] = { { 3, 0, 6, 5, 0, 8, 4, 0, 0 },
69-
{ 5, 2, 0, 0, 0, 0, 0, 0, 0 },
70-
{ 0, 8, 7, 0, 0, 0, 0, 3, 1 },
71-
{ 0, 0, 3, 0, 1, 0, 0, 8, 0 },
72-
{ 9, 0, 0, 8, 6, 3, 0, 0, 5 },
73-
{ 0, 5, 0, 0, 9, 0, 6, 0, 0 },
74-
{ 1, 3, 0, 0, 0, 0, 2, 5, 0 },
75-
{ 0, 0, 0, 0, 0, 0, 0, 7, 4 },
76-
{ 0, 0, 5, 2, 0, 6, 3, 0, 0 } };
77-
if (sudokuSolver(sudoku, 0, 0)) {
65+
int[][] sudoku = {
66+
{ 5, 3, 0, 0, 7, 0, 0, 0, 0 },
67+
{ 6, 0, 0, 1, 9, 5, 0, 0, 0 },
68+
{ 0, 9, 8, 0, 0, 0, 0, 6, 0 },
69+
{ 8, 0, 0, 0, 6, 0, 0, 0, 3 },
70+
{ 4, 0, 0, 8, 0, 3, 0, 0, 1 },
71+
{ 7, 0, 0, 0, 2, 0, 0, 0, 6 },
72+
{ 0, 6, 0, 0, 0, 0, 2, 8, 0 },
73+
{ 0, 0, 0, 4, 1, 9, 0, 0, 5 },
74+
{ 0, 0, 0, 0, 8, 0, 0, 7, 9 }
75+
};
7876

79-
print(sudoku);
77+
if (sudokuSolver(sudoku, 0, 0)) {
78+
System.out.println("Sudoku Solved:");
79+
printSudoku(sudoku);
8080
} else {
81-
System.out.println("not valid");
81+
System.out.println("No solution exists.");
8282
}
8383
}
84-
8584
}

0 commit comments

Comments
 (0)