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
129 lines (115 loc) · 4.03 KB
/
SudokuSolver.java
File metadata and controls
129 lines (115 loc) · 4.03 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
119
120
121
122
123
124
125
126
127
128
129
package com.thealgorithms.backtracking;
/**
* Sudoku Solver using Backtracking algorithm
* Solves a 9x9 Sudoku puzzle using depth-first search with backtracking
*
* @author Your Name
*/
public final class SudokuSolver {
private static final int BOARD_SIZE = 9;
private static final int SUBGRID_SIZE = 3;
private static final int EMPTY_CELL = 0;
private SudokuSolver() {
}
/**
* Solves the Sudoku puzzle using backtracking
*
* @param board 9x9 Sudoku board (0 represents empty cells)
* @return true if puzzle is solvable, false otherwise
*/
public static boolean solveSudoku(int[][] board) {
for (int row = 0; row < BOARD_SIZE; row++) {
for (int col = 0; col < BOARD_SIZE; col++) {
if (board[row][col] == EMPTY_CELL) {
for (int num = 1; num <= BOARD_SIZE; num++) {
if (isValid(board, row, col, num)) {
board[row][col] = num;
if (solveSudoku(board)) {
return true;
}
board[row][col] = EMPTY_CELL;
}
}
return false;
}
}
}
return true;
}
/**
* Checks if placing a number at a given position is valid
*/
private static boolean isValid(int[][] board, int row, int col, int num) {
return isRowOk(board, row, num) && isColOk(board, col, num) && isSubgridOk(board, row, col, num);
}
/**
* Checks if number exists in the row
*/
private static boolean isRowOk(int[][] board, int row, int num) {
for (int col = 0; col < BOARD_SIZE; col++) {
if (board[row][col] == num) {
return false;
}
}
return true;
}
/**
* Checks if number exists in the column
*/
private static boolean isColOk(int[][] board, int col, int num) {
for (int row = 0; row < BOARD_SIZE; row++) {
if (board[row][col] == num) {
return false;
}
}
return true;
}
/**
* Checks if number exists in the 3x3 subgrid
*/
private static boolean isSubgridOk(int[][] board, int row, int col, int num) {
int subgridRowStart = row - row % SUBGRID_SIZE;
int subgridColStart = col - col % SUBGRID_SIZE;
for (int r = subgridRowStart; r < subgridRowStart + SUBGRID_SIZE; r++) {
for (int c = subgridColStart; c < subgridColStart + SUBGRID_SIZE; c++) {
if (board[r][c] == num) {
return false;
}
}
}
return true;
}
/**
* Prints the Sudoku board
*/
private static void printBoard(int[][] board) {
for (int row = 0; row < BOARD_SIZE; row++) {
if (row % 3 == 0 && row != 0) {
System.out.println("------+-------+------");
}
for (int col = 0; col < BOARD_SIZE; col++) {
if (col % 3 == 0 && col != 0) {
System.out.print("| ");
}
System.out.print(board[row][col]);
}
System.out.println();
}
}
/**
* Main method demonstrating Sudoku solver functionality
*
* @param args command line arguments (not used)
*/
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}};
System.out.println("Sudoku Puzzle:");
printBoard(board);
if (solveSudoku(board)) {
System.out.println("\nSolved Sudoku:");
printBoard(board);
} else {
System.out.println("\nNo solution exists for this Sudoku puzzle.");
}
}
}