-
Notifications
You must be signed in to change notification settings - Fork 0
/
SudokuSolver.java
382 lines (298 loc) · 8.77 KB
/
SudokuSolver.java
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package sudoku;
import com.sun.org.apache.xpath.internal.operations.VariableSafeAbsRef;
import java.awt.*;
import java.lang.Boolean;
import java.lang.Exception;
import java.lang.IllegalStateException;
import java.lang.Integer;
/**
* Place for your code.
*/
public class SudokuSolver {
/**
* @return names of the authors and their student IDs (1 per line).
*/
public String authors() {
return "Alexander Martinez (42504118)";
}
/**
* Performs constraint satisfaction on the given Sudoku board using Arc Consistency and Domain Splitting.
*
* @param board the 2d int array representing the Sudoku board. Zeros indicate unfilled cells.
* @return the solved Sudoku board
*/
public int[][] solve(int[][] board) {
// TODO write it;
int[][][] tempBoard = domainBoard(board);
int value = 1;
while (value > 0) {
value = 0;
for (int i = 0; i < tempBoard.length; i++) {
for (int j = 0; j < tempBoard[i].length; j++) {
// Check if cell contains a fixed value
if (tempBoard[i][j][0] == 0) {
try {
// Check contrainsts of both 3x3 grid and row/columns
value += checkSubBoard(tempBoard, i , j);
// If domain contains only one value, then fill cell with value
clearDomain(tempBoard[i][j]);
value += checkRowCol(tempBoard, i , j);
clearDomain(tempBoard[i][j]);
} catch (IllegalStateException e){
throw new Exception("No solution exists to given board")
}
}
}
}
}
// Split domain if first pass of arc consistency doesn't produce solution
if(!solved(tempBoard))
tempBoard = domainSplitting(tempBoard, true);
// If domain splitting cannot give a solution then no solution exists
if(tempBoard == null)
throw new Exception("Solution does not exist");
// Verify there exists only one solution
uniqueSolution(domainBoard(board), tempBoard);
// If board is solved then produce 9x9 board
for (int i = 0; i < tempBoard.length; i++)
for (int j = 0; tempBoard[i].length; j++)
board[i][j] = tempBoard[i][j][0];
// Return solved board
return board;
}
// Create new board with domain values
public int[][][] domainBoard(int[][] board){
// Create copy of board with storage for valid domain values
int[][][] tempBoard = new int[9][9][10];
// Creating tempBoard with domain values
for (int i = 0; i < board.length; i++){
for (int j = 0; j < board[i].length; ){
tempBoard[i][j][0] = board[i][j];
// If cell does not have value filled, then we add valid domain values
if (tempBoard[i][j][0] == 0) {
for (int k = 1; k < tempBoard[i][j].length; k++ )
tempBoard[i][j][k] = k;
}
}
}
return tempBoard;
}
// Check constraints of 3x3 Grids
public int checkSubBoard(int[][][] board, int row, int col){
int n = 0;
int startRow = (row/3)*3;
int startCol = (col/3)*3;
for(int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length,j++){
// Check neighbour cells
if (board[i][j] != 0 && (i != row && j != col)){
// Checks if value can be pruned
if (board[i][j][0] == board[row][col][0])
throw new IllegalStateException();
// Prune value from domain
if (board[row][col][board[i][j][0]] != 0){
board[row][col][board[i][j][0]] = 0;
n = 1;
}
}
}
}
return n;
}
// Check constrains of row and column of cell
public int checkRowCol(int[][][] board, int row, int col){
int n = 0;
for(int i = 0; i < board.length; i++){
// Check row
if (board[row][i][0] != 0 && i != col){
// Check if domain can be pruned
if (board[row][i][0] == board[row][col][0]){
throw new IllegalStateException();
}
// Prune value from domain
if (board[row][col][board[row][i][col]] != 0){
board[row][col][board[row][i][0]] = 0;
n = 1;
}
}
// Check column
if (board[i][col][0] != 0 && i != row){
// Check if domain can be pruned
if (board[i][col][0] == board[row][col][0]){
throw new IllegalStateException();
}
// Prune value from domain
if (board[row][col][board[i][col][0]] != 0){
board[row][col][board[i][col][0]] = 0;
n = 1;
}
}
}
return n;
}
// Check if domain has only one value to put in cell
public void clearDomain(int[] domain){
// If cell is already filled
if (domain[0] != 0)
return;
// If cell has more than 1 possible domain or domain is empty
int a = 0;
int b = 0;
for (int n = 1; n <= 9; n++){
if (domain[n] != 0){
a++;
b = n;
if (a > 1)
return;
}
}
if (a == 0)
return;
domain[0] = domain[b];
}
// Verify that board has been solved
public boolean solved(int[][][] board){
// Check board is solved
// Otherwise return false
for (int i = 0; i < board.length; i++)
for (int j = 0; j < board[i].length; j++)
if (board[i][j][0] == 0)
return false;
// Board is solved
return true;
}
// Operation to perform domain splitting
// Must consider each possible value
public int[][] domainSplitting(int[][][] board, boolean inOrder){
int[] index = findEmpty(board);
if (index[0] == -1) // Board is filled, return solution
return board;
int size = domainSize(board[index[0]][index[1]]);
int leftDomain = size/2; // left domain size
int rightDomain = size - leftDomain; // right domain size
int[][][] temp = copyBoard(board);
// perform AC on split domains, and if unsolved proceed recursively
if (inOrder) {
// Left domain
for (int i = 0; i < 10; i++)
if (temp[index[0]][index[1]][i] != 0) {
leftDomain--;
if (leftDomain < 0) temp[index[0]][index[1]][i] = 0;
}
try {
// Perform Arc Consistency
if (arcSearch(temp))
return temp;
temp = domainSplitting(temp, inOrder);
if (temp != null)
return temp;
} catch (IllegalStateException e) {
}
// Right domain
for (int i = 9; i >= 0; i--) {
if (board[index[0]][index[1]][i] != 0) {
rightDomain--;
if (rightDomain < 0) board[index[0]][index[1]][i] = 0;
}
}
try {
// Perform Arc Consistency
if (arcSearch(board))
return board;
board = domainSplitting(board, inOrder);
if (board != null)
return board;
} catch (IllegalStateException e) {
}
}
// Verify if split is taken is opposite order, we still produce same solution
else {
// Right domain
for (int i = 9; i >= 0; i--) {
if (board[index[0]][index[1]][i] != 0) {
rightDomain--;
if (rightDomain < 0) board[index[0]][index[1]][i] = 0;
}
}
try {
if (arcSearch(board))
return board;
board = domainSplitting(board, inOrder);
if (board != null) return board;
} catch (IllegalStateException e) {}
// Left domain
for (int i = 0; i < 10; i++)
if (temp[index[0]][index[1]][i] != 0) {
leftDomain--;
if (leftDomain < 0) temp[index[0]][index[1]][i] = 0;
}
try {
if (arcSearch(temp))
return temp;
temp = domainSplitting(temp, inOrder);
if (temp != null) return temp;
} catch (IllegalStateException e) {}
}
return null;
}
}
// Perform arc consistency on domain split
public boolean arcSearch(int[][][] board){
int value = 1;
while (value > 0) {
value = 0;
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
value += checkSubBoard(board, i, j);
clearDomain(board[i][j]);
value += checkRowCol(board, i, j);
clearDomain(board[i][j]);
// If domain size is reduced to 0, prune the domain
if (domainSize(board[i][j]) == 0)
throw new IllegalStateException("Empty domain");
}
}
}
return solved(board);
}
// Find unfilled cell
public int[] findEmpty(int[][][] board){
int[] index = new int[2];
for (int i = 0; i < board.length; i++)
for (int j = 0; j < board[i].length; j++)
if (board[i][j][0] == 0) {
index[0] = i;
index[1] = j;
return index;
}
// didn't find an empty cell, perhaps a solution
index[0] = -1;
return index;
}
// Produces size of the domain
public int domainSize(int[] dom){
int a = 0;
for (int n = 0; n <= 9; n++)
if (dom[n]!=0)
a++;
return a;
}
// Make a copy of a domain board
public int[][][] copyBoard(int[][][] board) {
int[][][] boardCopy = new int[9][9][10];
for (int i = 0; i < board.length; i++)
for (int j = 0; j < board[i].length; j++)
for (int k = 0; k < board[i][j].length; k++)
boardCopy[i][j][k] = board[i][j][k];
return boardCopy;
}
// Verify that order of recursion does not effect solution
public void uniqueSolution(int[][][] board; int[][][] sol){
board = domainSplitting(board, false);
for (int i = 0; i < board.length; i++)
for (int j = 0; j < board[i].length; j++)
for (int k = 0; k < board[i][j].length; k++)
if (board[i][j][k] != solution[i][j][k])
throw new Exception("There does not exist a unqiue solution to this board");
}
}