forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudokuSolver.java
118 lines (104 loc) · 3.47 KB
/
SudokuSolver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
public class SudokuPuzzle {
// Check if it's safe to place 'n' in the cell at row 'r' and column 'c'
public boolean isSafe(int[][] board, int r, int c, int n) {
// Check for clashes in the row
for (int d = 0; d < board.length; d++) {
if (board[r][d] == n) {
return false;
}
}
// Check for clashes in the column
for (int r1 = 0; r1 < board.length; r1++) {
if (board[r1][c] == n) {
return false;
}
}
// Check for clashes in the sub-grid
int sqrt = (int) Math.sqrt(board.length);
int boxRowStart = r - r % sqrt;
int boxColStart = c - c % sqrt;
for (int r1 = boxRowStart; r1 < boxRowStart + sqrt; r1++) {
for (int d = boxColStart; d < boxColStart + sqrt; d++) {
if (board[r1][d] == n) {
return false;
}
}
}
return true;
}
// Solve the Sudoku puzzle using backtracking
public boolean solveSudoku(int[][] board, int num) {
int r = -1;
int c = -1;
boolean isVacant = true;
for (int i = 0; i < num; i++) {
for (int j = 0; j < num; j++) {
if (board[i][j] == 0) {
r = i;
c = j;
isVacant = false;
break;
}
}
if (!isVacant) {
break;
}
}
if (isVacant) {
return true; // No empty cells left, the puzzle is solved
}
// Try placing numbers from 1 to num in the cell
for (int no = 1; no <= num; no++) {
if (isSafe(board, r, c, no)) {
board[r][c] = no;
if (solveSudoku(board, num)) {
return true;
} else {
board[r][c] = 0; // Backtrack
}
}
}
return false; // No solution found for the current configuration
}
// Display the Sudoku grid
public void display(int[][] board, int n) {
for (int i = 0; i < n; i++) {
for (int d = 0; d < n; d++) {
System.out.print(board[i][d] + " ");
}
System.out.println();
if ((i + 1) % (int) Math.sqrt(n) == 0) {
System.out.println();
}
}
}
public static void main(String[] args) {
int[][] board = new int[][] {
{7, 0, 0, 0, 0, 0, 2, 0, 0},
{4, 0, 2, 0, 0, 0, 0, 0, 3},
{0, 0, 0, 2, 0, 1, 0, 0, 0},
{3, 0, 0, 1, 8, 0, 0, 9, 7},
{0, 0, 9, 0, 7, 0, 6, 0, 0},
{6, 5, 0, 0, 3, 2, 0, 0, 1},
{0, 0, 0, 4, 0, 9, 0, 0, 0},
{5, 0, 0, 0, 0, 0, 1, 0, 6},
{0, 0, 6, 0, 0, 0, 0, 0, 8}
};
SudokuPuzzle obj = new SudokuPuzzle();
int size = board.length;
System.out.println("The input grid is:");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
System.out.println();
if (obj.solveSudoku(board, size)) {
System.out.println("The solution to the Sudoku puzzle is:");
obj.display(board, size);
} else {
System.out.println("There is no solution available.");
}
}
}