forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSudokuSolver.java
More file actions
118 lines (101 loc) · 3.34 KB
/
SudokuSolver.java
File metadata and controls
118 lines (101 loc) · 3.34 KB
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
package com.thealgorithms.backtracking;
/**
* Program description - Solves a given 9x9 Sudoku puzzle using backtracking.
* <a href="https://en.wikipedia.org/wiki/Sudoku">Wikipedia</a>
*
* @author <a href="https://github.com/codewithRakshita">Rakshita</a>
*/
public class SudokuSolver {
private static final int SIZE = 9; // size of the Sudoku grid
/**
* Solves the Sudoku puzzle by filling empty cells.
*
* @param board The 9x9 Sudoku puzzle grid
* @return true if the Sudoku puzzle is solvable, false otherwise
*/
public boolean solveSudoku(int[][] board) {
for (int row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++) {
// Check for an empty cell
if (board[row][col] == 0) {
// Try numbers 1 to 9
for (int num = 1; num <= SIZE; num++) {
if (isSafe(board, row, col, num)) {
board[row][col] = num;
// Recursively try filling the rest
if (solveSudoku(board)) {
return true;
}
// Backtrack
board[row][col] = 0;
}
}
// No valid number found — backtrack
return false;
}
}
}
return true; // solved
}
/**
* Checks whether placing a number at a specific position is valid.
*/
private boolean isSafe(int[][] board, int row, int col, int num) {
// Check row
for (int x = 0; x < SIZE; x++) {
if (board[row][x] == num) {
return false;
}
}
// Check column
for (int x = 0; x < SIZE; x++) {
if (board[x][col] == num) {
return false;
}
}
// Check 3x3 sub-grid
int startRow = row - row % 3;
int startCol = col - col % 3;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i + startRow][j + startCol] == num) {
return false;
}
}
}
return true;
}
/**
* Prints the Sudoku grid.
*/
public void printBoard(int[][] board) {
for (int r = 0; r < SIZE; r++) {
for (int d = 0; d < SIZE; d++) {
System.out.print(board[r][d] + " ");
}
System.out.println();
}
System.out.println();
}
// Example usage (for testing)
public static void main(String[] args) {
int[][] board = {
{5, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}
};
SudokuSolver solver = new SudokuSolver();
if (solver.solveSudoku(board)) {
System.out.println("Solved Sudoku:");
solver.printBoard(board);
} else {
System.out.println("No solution exists.");
}
}
}