- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSudoku.java
More file actions
Latest commit
130 lines (116 loc) · 3.66 KB
/
Copy pathSudoku.java
File metadata and controls
130 lines (116 loc) · 3.66 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
130
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.util.StringTokenizer;
publicclassSudoku {
privatestaticintsudokuMatrix[][];
privatestaticbooleanskip[][];
publicstaticbooleanisValidPlacement(introw, intcol) {
// Row inspection
intvalue = sudokuMatrix[row][col];
for (inti = 0; i < 9; i++) {
if (i == col)
continue;
elseif (sudokuMatrix[row][i] == value) {
returnfalse;
}
}
// Column inspection
for (inti = 0; i < 9; i++) {
if (i == row)
continue;
elseif (sudokuMatrix[i][col] == value) {
returnfalse;
}
}
// 3x3 Small Grid inspection
intbaseRow = (row/3)*3;
intbaseCol = (col/3)*3;
for (inti = baseRow; i < baseRow+3 ; i++) {
for (intj = baseCol; j < baseCol+3 ; j++) {
if (i == row && j == col)
continue;
elseif (sudokuMatrix[i][j] == value)
returnfalse;
}
}
returntrue;
}
publicstaticbooleansolve(introw, intcol) {
// Final Goal Case
if (row == 9)
returntrue;
if (skip[row][col]) {
if (col+1 != 9){
if (solve(row, col+1))
returntrue;
}
else {
if (solve(row+1, 0))
returntrue;
}
} else {
for (inti = 1; i < 10; i++) {
sudokuMatrix[row][col] = i;
if (isValidPlacement(row, col)) {
if (col+1 != 9){
if (solve(row, col+1))
returntrue;
}
else {
if (solve(row+1, 0))
returntrue;
}
}
}
sudokuMatrix[row][col] = 0;
}
returnfalse;
}
publicstaticvoidmain(String[] args)throwsIOException {
BufferedReaderbr = newBufferedReader(newInputStreamReader(System.in));
sudokuMatrix = newint[9][9];
skip = newboolean[9][9];
// Sample Input:
// 3 0 6 5 0 8 4 0 0
// 5 2 0 0 0 0 0 0 0
// 0 8 7 0 0 0 0 3 1
// 0 0 3 0 1 0 0 8 0
// 9 0 0 8 6 3 0 0 5
// 0 5 0 0 9 0 6 0 0
// 1 3 0 0 0 0 2 5 0
// 0 0 0 0 0 0 0 7 4
// 0 0 5 2 0 6 3 0 0
for (inti = 0; i < 9; i++) {
StringTokenizerst = newStringTokenizer(br.readLine(), " ");
for (intj = 0; j < 9; j++) {
sudokuMatrix[i][j] = Integer.parseInt(st.nextToken());
if (sudokuMatrix[i][j] > 0)
skip[i][j] = true;
}
}
// Sample Output:
// The Sudoku have been solved!
//
// 3 1 6 5 7 8 4 9 2
// 5 2 9 1 3 4 7 6 8
// 4 8 7 6 2 9 5 3 1
// 2 6 3 4 1 5 9 8 7
// 9 7 4 8 6 3 1 2 5
// 8 5 1 7 9 2 6 4 3
// 1 3 8 9 4 7 2 5 6
// 6 9 2 3 5 1 8 7 4
// 7 4 5 2 8 6 3 1 9
if (solve(0, 0)){
System.out.println("The Sudoku have been solved!" + "\n");
for (inti = 0; i < 9; i++) {
for (intj = 0; j < 9; j++) {
System.out.print(sudokuMatrix[i][j] + " ");
}
System.out.println();
}
}else {
System.out.println("The Sudoku could not be solved!");
}
}
}