Skip to content

Commit 2aafdeb

Browse files
authored
Refactor Sudoku Solver for Improved Readability and Maintainability (#3566)
2 parents 1df6e6d + 22cc8f4 commit 2aafdeb

File tree

1 file changed

+52
-27
lines changed

1 file changed

+52
-27
lines changed

JavaSolutions/SudokoSolver.java

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
public class SudokuSolver {
2+
3+
private static final int SIZE = 9;
4+
25
public static boolean solve(int[][] board) {
3-
for (int i = 0; i < 9; i++) {
4-
for (int j = 0; j < 9; j++) {
5-
if (board[i][j] == 0) {
6-
for (int num = 1; num <= 9; num++) {
7-
if (isSafe(board, i, j, num)) {
8-
board[i][j] = num;
9-
if (solve(board))
6+
for (int row = 0; row < SIZE; row++) {
7+
for (int col = 0; col < SIZE; col++) {
8+
if (board[row][col] == 0) {
9+
for (int num = 1; num <= SIZE; num++) {
10+
if (isSafe(board, row, col, num)) {
11+
board[row][col] = num;
12+
13+
if (solve(board)) {
1014
return true;
11-
board[i][j] = 0; // backtrack
15+
}
16+
17+
board[row][col] = 0; // Backtrack
1218
}
1319
}
1420
return false;
@@ -19,35 +25,54 @@ public static boolean solve(int[][] board) {
1925
}
2026

2127
private static boolean isSafe(int[][] board, int row, int col, int num) {
22-
for (int k = 0; k < 9; k++)
23-
if (board[row][k] == num || board[k][col] == num)
28+
// Check row and column
29+
for (int i = 0; i < SIZE; i++) {
30+
if (board[row][i] == num || board[i][col] == num) {
2431
return false;
32+
}
33+
}
34+
35+
// Check 3x3 subgrid
36+
int startRow = row - row % 3;
37+
int startCol = col - col % 3;
2538

26-
int sr = row - row % 3, sc = col - col % 3;
27-
for (int r = 0; r < 3; r++)
28-
for (int c = 0; c < 3; c++)
29-
if (board[sr + r][sc + c] == num)
39+
for (int r = 0; r < 3; r++) {
40+
for (int c = 0; c < 3; c++) {
41+
if (board[startRow + r][startCol + c] == num) {
3042
return false;
43+
}
44+
}
45+
}
3146

3247
return true;
3348
}
3449

50+
private static void printBoard(int[][] board) {
51+
for (int[] row : board) {
52+
for (int num : row) {
53+
System.out.print(num + " ");
54+
}
55+
System.out.println();
56+
}
57+
}
58+
3559
public static void main(String[] args) {
3660
int[][] board = {
37-
{5,3,0,0,7,0,0,0,0},
38-
{6,0,0,1,9,5,0,0,0},
39-
{0,9,8,0,0,0,0,6,0},
40-
{8,0,0,0,6,0,0,0,3},
41-
{4,0,0,8,0,3,0,0,1},
42-
{7,0,0,0,2,0,0,0,6},
43-
{0,6,0,0,0,0,2,8,0},
44-
{0,0,0,4,1,9,0,0,5},
45-
{0,0,0,0,8,0,0,7,9}
61+
{5, 3, 0, 0, 7, 0, 0, 0, 0},
62+
{6, 0, 0, 1, 9, 5, 0, 0, 0},
63+
{0, 9, 8, 0, 0, 0, 0, 6, 0},
64+
{8, 0, 0, 0, 6, 0, 0, 0, 3},
65+
{4, 0, 0, 8, 0, 3, 0, 0, 1},
66+
{7, 0, 0, 0, 2, 0, 0, 0, 6},
67+
{0, 6, 0, 0, 0, 0, 2, 8, 0},
68+
{0, 0, 0, 4, 1, 9, 0, 0, 5},
69+
{0, 0, 0, 0, 8, 0, 0, 7, 9}
4670
};
47-
solve(board);
48-
for (int[] row : board) {
49-
for (int num : row) System.out.print(num + " ");
50-
System.out.println();
71+
72+
if (solve(board)) {
73+
printBoard(board);
74+
} else {
75+
System.out.println("No solution exists.");
5176
}
5277
}
5378
}

0 commit comments

Comments
 (0)