forked from DhanushNehru/Hacktoberfest2025
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSudoku_Solver.java
More file actions
69 lines (64 loc) · 2.61 KB
/
Sudoku_Solver.java
File metadata and controls
69 lines (64 loc) · 2.61 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
public class SudokuSolver {
// Function to check if placing num on the board[row][col] is valid
private static boolean isValid(char[][] board, int row, int col, char num) {
for (int i = 0; i < 9; i++) {
// Check if num is in the current row, column, or sub-box
if (board[row][i] == num || board[i][col] == num ||
board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] == num) {
return false;
}
}
return true;
}
// Backtracking function to solve the Sudoku board
private static boolean solveSudoku(char[][] board) {
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
// If the current cell is empty
if (board[row][col] == '.') {
// Try all numbers from 1 to 9
for (char num = '1'; num <= '9'; num++) {
if (isValid(board, row, col, num)) {
board[row][col] = num;
if (solveSudoku(board)) {
return true; // If the board is solved, return true
}
board[row][col] = '.'; // Backtrack
}
}
return false; // No solution found, return false
}
}
}
return true; // Sudoku solved
}
// Utility function to print the Sudoku board
private static void printBoard(char[][] board) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
// Sample Sudoku board with empty cells denoted by '.'
char[][] board = {
{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
{'.', '.', '.', '.', '8', '.', '.', '7', '9'}
};
if (solveSudoku(board)) {
System.out.println("Sudoku solved successfully!");
printBoard(board);
} else {
System.out.println("No solution exists.");
}
}
}