-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSudokuSolver.java
More file actions
184 lines (163 loc) · 8.71 KB
/
SudokuSolver.java
File metadata and controls
184 lines (163 loc) · 8.71 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package backtracking;
// Source : https://leetcode.com/problems/sudoku-solver/
// Id : 37
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Topic : Backtracking
// Level : Hard
// Date : 2020-01-15
// Other :
// Tips :
// Result : 97.00% 94.25%
public class SudokuSolver {
int redoCount = 0;
// 90.95% 4ms 74.92%
public void solveSudoku(char[][] board) {
boolean rowState[][] = new boolean[9][10];
boolean columnState[][] = new boolean[9][10];
boolean boxState[][] = new boolean[9][10];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j] != '.') {
int index = board[i][j] - '0';
rowState[i][index] = true;
columnState[j][index] = true;
/**
* Box state represented like below
* 0 1 2
* 3 4 5
* 6 7 8
*/
boxState[(i / 3) * 3 + j / 3][index] = true;
}
}
}
recursivePlaceNumber(board, rowState, columnState, boxState, 0, 0);
}
private boolean recursivePlaceNumber(char[][] board, boolean[][] rowState, boolean[][] columnState, boolean[][] boxState, int row, int column) {
if (column == 9) {
column = 0;
row++;
if (row == 9) {
// game complete
return true;
}
}
if (board[row][column] != '.') {
return recursivePlaceNumber(board, rowState, columnState, boxState, row, column + 1);
} else {
for (int i = 1; i < 10; i++) {
if (rowState[row][i] || columnState[column][i] || boxState[(row / 3) * 3 + column / 3][i]) {
continue;
} else {
placeNumber(board, rowState, columnState, boxState, row, column, i);
if (recursivePlaceNumber(board, rowState, columnState, boxState, row, column + 1)) {
return true;
}
undoNumberPlacement(board, rowState, columnState, boxState, row, column, i);
}
}
}
// failed to get an answer
return false;
}
private void placeNumber(char[][] board, boolean[][] rowState, boolean[][] columnState, boolean[][] boxState, int row, int column, int i) {
rowState[row][i] = true;
columnState[column][i] = true;
boxState[(row / 3) * 3 + column / 3][i] = true;
board[row][column] = (char) ('0' + i);
redoCount++;
}
private void undoNumberPlacement(char[][] board, boolean[][] rowState, boolean[][] columnState, boolean[][] boxState, int row, int column, int i) {
rowState[row][i] = false;
columnState[column][i] = false;
boxState[(row / 3) * 3 + column / 3][i] = false;
board[row][column] = '.';
}
/**
* start from the most possible place
*
* @param board
*/
// 97.00% 2ms 94.25%
public void solveSudoku2(char[][] board) {
// public void solveSudoku(char[][] board) {
boolean rowState[][] = new boolean[9][10];
boolean columnState[][] = new boolean[9][10];
boolean boxState[][] = new boolean[9][10];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j] != '.') {
int index = board[i][j] - '0';
rowState[i][index] = true;
columnState[j][index] = true;
/**
* Box state represented like below
* 0 1 2
* 3 4 5
* 6 7 8
*/
boxState[(i / 3) * 3 + j / 3][index] = true;
}
}
}
int[] coordinate = getMaxPossibleCoordinate(board, rowState, columnState, boxState);
recursivePlaceNumber2(board, rowState, columnState, boxState, coordinate[0], coordinate[1]);
}
private int[] getMaxPossibleCoordinate(char[][] board, boolean[][] rowState, boolean[][] columnState, boolean[][] boxState) {
int x = -1, y = -1, minCount = 9;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j] != '.') {
continue;
}
int tmpCount = 9;
for (int k = 0; k < 9; k++) {
if (rowState[i][k] || columnState[j][k] || boxState[(i / 3) * 3 + j / 3][k])
tmpCount--;
}
if (tmpCount == 1)
return new int[]{i, j};
if (minCount > tmpCount) {
minCount = tmpCount;
x = i;
y = j;
}
}
}
return new int[]{x, y};
}
private boolean recursivePlaceNumber2(char[][] board, boolean[][] rowState, boolean[][] columnState, boolean[][] boxState, int row, int column) {
if (row == -1 && column == -1)
return true;
if (board[row][column] != '.')
return false;
for (int i = 1; i < 10; i++) {
if (rowState[row][i] || columnState[column][i] || boxState[(row / 3) * 3 + column / 3][i]) {
continue;
} else {
placeNumber(board, rowState, columnState, boxState, row, column, i);
int[] coordinate = getMaxPossibleCoordinate(board, rowState, columnState, boxState);
if (recursivePlaceNumber2(board, rowState, columnState, boxState, coordinate[0], coordinate[1])) {
return true;
}
undoNumberPlacement(board, rowState, columnState, boxState, row, column, i);
}
}
// failed to get an answer
return false;
}
public static void main(String[] args) {
SudokuSolver s = new SudokuSolver();
// s.solveSudoku(new char[][]{{'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'}});
// s.solveSudoku(new char[][]{{'5','3','4','6','7','8','9','1','2'},{'6','7','2','1','9','5','3','4','8'},{'1','9','8','3','4','2','5','6','7'},{'8','5','9','7','6','1','4','2','3'},{'4','2','6','8','5','3','7','9','1'},{'7','1','3','9','2','4','8','5','6'},{'9','6','1','5','3','7','2','8','4'},{'2','8','7','4','1','9','6','3','5'},{'3','4','5','2','8','6','1','7','9'}});
// s.solveSudoku(new char[][]{{'5', '3', '.', '.', '7', '8', '9', '1', '2'}, {'6', '7', '2', '1', '9', '5', '3', '4', '8'}, {'1', '9', '8', '3', '4', '2', '5', '6', '7'}, {'8', '5', '9', '7', '6', '1', '4', '2', '3'}, {'4', '2', '6', '8', '5', '3', '7', '9', '1'}, {'7', '1', '3', '9', '2', '4', '8', '5', '6'}, {'9', '6', '1', '5', '3', '7', '2', '8', '4'}, {'2', '8', '7', '4', '1', '9', '6', '3', '5'}, {'3', '4', '5', '2', '8', '6', '1', '7', '9'}});
// System.out.println(s.redoCount);
// s.redoCount =0;
char[][] c = new char[][]{{'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'}};
s.solveSudoku(new char[][]{{'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'}});
System.out.println(s.redoCount);
s.redoCount =0;
s.solveSudoku2(new char[][]{{'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'}});
System.out.println(s.redoCount);
}
}