-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSudokuSolver.java
More file actions
116 lines (102 loc) · 3.97 KB
/
SudokuSolver.java
File metadata and controls
116 lines (102 loc) · 3.97 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
import java.util.Scanner;
public class SudokuSolver {
private static final int GRID_SIZE = 9;
public static void main(String[] args) {
int[][] board = new int[GRID_SIZE][GRID_SIZE];
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the Sudoku puzzle row by row (use 0 for empty cells):");
for (int row = 0; row < GRID_SIZE; row++) {
while (true) {
System.out.printf("Enter row %d (9 numbers separated by spaces): ", row + 1);
String input = scanner.nextLine();
String[] values = input.split(" ");
if (values.length == GRID_SIZE) {
boolean validRow = true;
for (int col = 0; col < GRID_SIZE; col++) {
try {
int value = Integer.parseInt(values[col]);
if (value < 0 || value > 9) {
System.out.println("Please enter numbers between 0 and 9.");
validRow = false;
break;
}
board[row][col] = value;
} catch (NumberFormatException e) {
System.out.println("Invalid input. Please enter integers only.");
validRow = false;
break;
}
}
if (validRow) {
break;
}
} else {
System.out.println("Please enter exactly 9 numbers.");
}
}
}
if (solveBoard(board)) {
System.out.println("Sudoku solved successfully!");
} else {
System.out.println("Unsolvable board.");
}
printBoard(board);
scanner.close();
}
private static boolean solveBoard(int[][] board) {
for (int row = 0; row < GRID_SIZE; row++) {
for (int col = 0; col < GRID_SIZE; col++) {
if (board[row][col] == 0) {
for (int num = 1; num <= GRID_SIZE; num++) {
if (isValid(board, num, row, col)) {
board[row][col] = num;
if (solveBoard(board)) {
return true;
} else {
board[row][col] = 0;
}
}
}
return false;
}
}
}
return true;
}
private static boolean isValid(int[][] board, int num, int row, int col) {
for (int i = 0; i < GRID_SIZE; i++) {
if (board[row][i] == num) {
return false;
}
}
for (int i = 0; i < GRID_SIZE; i++) {
if (board[i][col] == num) {
return false;
}
}
int subGridRowStart = row - row % 3;
int subGridColStart = col - col % 3;
for (int i = subGridRowStart; i < subGridRowStart + 3; i++) {
for (int j = subGridColStart; j < subGridColStart + 3; j++) {
if (board[i][j] == num) {
return false;
}
}
}
return true;
}
private static void printBoard(int[][] board) {
for (int row = 0; row < GRID_SIZE; row++) {
if (row % 3 == 0 && row != 0) {
System.out.println("-----------");
}
for (int col = 0; col < GRID_SIZE; col++) {
if (col % 3 == 0 && col != 0) {
System.out.print("|");
}
System.out.print(board[row][col]);
}
System.out.println();
}
}
}