-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSudoku.java
More file actions
278 lines (229 loc) · 9.41 KB
/
Sudoku.java
File metadata and controls
278 lines (229 loc) · 9.41 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//Write a program to solve a Sudoku puzzle by filling the empty cells.
//
// A sudoku solution must satisfy all the following rules:
//
// Each of the digits 1-9 must occur exactly once in each row.
// Each of the digits 1-9 must occur exactly once in each column.
// Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.
// The '.' character indicates empty cells.
//Input:
// 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"]]
//
//Output: [["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"]]
import util.Utils;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import static resources.ConsoleColors.*;
import static resources.SudokuBoards.*;
public class Sudoku {
int depth;
List<Cell> cells; // search space, int[] for interview, class is better
int[] rows, cols, sqrs; // for each row, col, box what numbers have been used
private final int ALL_ON = (1 << 9) - 1; // all 1s, meaning all numbers available
public static void main(String[] args) {
// solve a sudoku and print the comparison
char[][] board = YUANHAO;
char[][] b2 = Utils.mxDeepCopy(board);
Sudoku sudoku = new Sudoku();
sudoku.solveSudoku2(b2);
sudoku.printSudoku(b2, board);
sudoku.printSudokuAsArray(b2, board);
char[][] b3 = YUANHAO;
boolean isValid = sudoku.isValidSudoku(b3);
sudoku.printValidSudoku(b3);
sudoku.printRes(isValid);
}
public void solveSudoku(char[][] board) {
depth = 0;
cells = new ArrayList<>();
rows = new int[9];Arrays.fill(rows, ALL_ON);
cols = new int[9];Arrays.fill(cols, ALL_ON);
sqrs = new int[9];Arrays.fill(sqrs, ALL_ON);
for (int r = 0; r < 9; r++) for (int c = 0; c < 9; c++) {
int s = (r / 3) * 3 + (c / 3);
if (board[r][c] == '.') cells.add(new Cell(r, c, s));
else {
int mask = 1 << (board[r][c] - '1');
rows[r] ^= mask;
cols[c] ^= mask;
sqrs[s] ^= mask;
}
}
solve(board, 0);
System.out.printf("\n" + BLUE + "Depth of Search: " + RED_BOLD_BRIGHT + "%,d" + RESET + '\n', depth);
}
private boolean solve(char[][] board, int i) {
if (i == cells.size()) return true;
depth++;
Cell c = cells.get(i);
int nums = rows[c.r] & cols[c.c] & sqrs[c.s];
while (nums != 0) {
int num = 31 - Integer.numberOfLeadingZeros(nums);
int mask = 1 << num;
board[c.r][c.c] = (char) ('1' + num);
rows[c.r] ^= mask;
cols[c.c] ^= mask;
sqrs[c.s] ^= mask;
if (solve(board, i + 1)) return true;
rows[c.r] |= mask; // 吃了🤮
cols[c.c] |= mask; // 吃了🤮
sqrs[c.s] |= mask; // 吃了🤮
nums ^= mask;
}
return false;
}
// TC: 9^81 → 9^n where n is the number of empty Cells
// SC: 3 * n + 27 + 81, what do you say this is…
// above are solution for Sudoku Solver only
// below are for Sudoku board validations
public boolean isValidSudoku(char[][] board) {
int N = 9;
int[] row = new int[N], col = new int[N], box = new int[N];
for (int r = 0; r < N; r++)
for (int c = 0; c < N; c++) {
if (board[r][c] == '.') continue;
int i = board[r][c] - '1';
int b = (r / 3) * 3 + (c / 3);
if (
((row[r] >> i) & 1) == 1 ||
((col[c] >> i) & 1) == 1 ||
((box[b] >> i) & 1) == 1
) return false;
row[r] |= 1 << i;
col[c] |= 1 << i;
box[b] |= 1 << i;
}
return true;
}
private void printRes(boolean isValid) {
String s = (isValid) ? "" : "NOT ";
System.out.println(RED_BOLD_BRIGHT + "\n\n" + s + "VALID" + RESET);
}
private void printValidSudoku(char[][] board) {
System.out.print("\n");
for (int r = 0; r < board.length; r++) {
for (int c = 0; c < board[r].length; c++) {
String coloredNumber = switch (board[r][c]) {
case '1' -> RED + board[r][c] + " " + RESET;
case '2', '8' -> GREEN + board[r][c] + " " + RESET;
case '3' -> YELLOW + board[r][c] + " " + RESET;
case '4' -> BLUE + board[r][c] + " " + RESET;
case '5' -> PURPLE + board[r][c] + " " + RESET;
case '6', '9' -> CYAN + board[r][c] + " " + RESET;
case '7' -> PURPLE_BOLD + board[r][c] + " " + RESET;
default -> ". ";
};
System.out.print(coloredNumber);
if ((c + 1) % 3 == 0 && c != 8) System.out.print("| ");
}
System.out.print("\n");
if ((r + 1) % 3 == 0 && r != 8) System.out.print("-------------------------------\n");
}
}
private void printSudoku(char[][] board, char[][] b2) {
System.out.print("\n");
for (int r = 0; r < board.length; r++) {
for (int c = 0; c < board[r].length; c++) {
if (board[r][c] == b2[r][c]) System.out.printf(RED_BOLD_BRIGHT + "%s " + RESET, board[r][c]);
else System.out.printf("%s ", board[r][c]);
if ((c + 1) % 3 == 0 && c != 8) System.out.print(CYAN + "| " + RESET);
}
System.out.println();
if ((r + 1) % 3 == 0 && r != 8) System.out.print(CYAN + "-------------------------------\n" + RESET);
}
System.out.println();
}
private void printSudokuAsArray(char[][] board, char[][] b2) {
System.out.print("\n{\n");
for (int r = 0; r < board.length; r++) {
System.out.print(" {");
for (int c = 0; c < board[r].length; c++) {
if (board[r][c] == b2[r][c]) System.out.printf(RED_BOLD_BRIGHT + "'%s', " + RESET, board[r][c]);
else System.out.printf("'%s', ", board[r][c]);
if ((c + 1) % 3 == 0 && c != 8) System.out.print(" ");
}
System.out.print("},\n");
if ((r + 1) % 3 == 0 && r != 8) System.out.print("\n");
}
System.out.print("}\n\n");
}
public void solveSudoku2(char[][] board) {
depth = 0;
cells = new ArrayList<>(81);
rows = new int[9];Arrays.fill(rows, ALL_ON);
cols = new int[9];Arrays.fill(cols, ALL_ON);
sqrs = new int[9];Arrays.fill(sqrs, ALL_ON);
for (int r = 0; r < 9; r++) for (int c = 0; c < 9; c++) {
int b = (r/3)*3 + (c/3);
if (board[r][c] != '.') {
int mask = 1 << (board[r][c] - '1');
rows[r] ^= mask;
cols[c] ^= mask;
sqrs[b] ^= mask;
} else cells.add(new Cell(r, c, b));
}
solve2(board, 0);
System.out.printf("\n" + BLUE + "Depth of Search: " + RED_BOLD_BRIGHT + "%,d" + RESET + '\n', depth);
}
private boolean solve2(char[][] board, int i) {
if (i == cells.size()) return true;
depth++;
int best = getBest(i);
if (best == -1) return false;
swapCell(i, best);
Cell c = cells.get(i);
int nums = rows[c.r] & cols[c.c] & sqrs[c.s];
while (nums != 0) {
int num = 31 - Integer.numberOfLeadingZeros(nums);
int mask = 1 << num;
board[c.r][c.c] = (char) ('1' + num);
rows[c.r] ^= mask;
cols[c.c] ^= mask;
sqrs[c.s] ^= mask;
if (solve2(board, i + 1)) return true;
rows[c.r] |= mask; // 吃了🤮
cols[c.c] |= mask; // 吃了🤮
sqrs[c.s] |= mask; // 吃了🤮
nums ^= mask;
}
return false;
}
private int getBest(int cur) {
int best = -1, nums = 10;
for (int i = cur; i < cells.size(); i++) {
Cell c = cells.get(i);
int mask = rows[c.r] & cols[c.c] & sqrs[c.s];
if (mask == 0) return -1;
int choices = Integer.bitCount(mask);
if (choices == 1) return i;
if (choices < nums) {
nums = choices;
best = i;
}
}
return best;
}
private void swapCell(int i, int j) {
Cell c = cells.get(i);
cells.set(i, cells.get(j));
cells.set(j, c);
}
record Cell(int r, int c, int s) {} // the row, col and box number for each cell
}