-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSolve_Sudoku.py
487 lines (355 loc) · 14.1 KB
/
Solve_Sudoku.py
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
import copy
def Cells(Grid):
global Empty_Cells, Filled_Cells
Empty_Cells = []
Filled_Cells = []
for i in range(9):
for j in range(9):
if Grid[i][j] == 0:
Empty_Cells.append([i, j])
else:
Filled_Cells.append([i, j])
def isValid(Grid, x, y, Value):
global X, Y
for i in range(9):
if Grid[i][y] == Value:
return False
if Grid[x][i] == Value:
return False
if Grid[X + int(i / 3)][Y + i % 3] == Value:
return False
return True
def Possibilities(Grid, x, y):
global X, Y
List = []
X, Y = int(x / 3) * 3, int(y / 3) * 3
for Value in range(1, 10):
if isValid(Grid, x, y, Value):
List.append(Value)
return List
def Constraints(Grid):
global Empty_Cells, Filled_Cells
List = [[0] * 9 for _ in range(9)]
for x, y in Empty_Cells:
List[x][y] = Possibilities(Grid, x, y)
for x, y in Filled_Cells:
List[x][y] = [Grid[x][y]]
return List
def Check(L):
Row_Check = False
Col_Check = False
Row = []
Col = []
for i in L:
Row.append(any(i))
for i in zip(*L):
Col.append(any(i))
R, C = 0, 0
R_N, C_N = -1, -1
for i in range(3):
if Row[i]:
R += 1
R_N = i
if Col[i]:
C += 1
C_N = i
if R == 1:
Row_Check = True
else:
R_N = -1
if C == 1:
Col_Check = True
else:
C_N = -1
return Row_Check, Col_Check, R_N, C_N
def Complete(L1, L2):
for i in L1:
if i not in L2:
return False
return True
def Partial(L1, L2):
for i in L1:
if i in L2:
return True
return False
def Update_Constraints(Grid, Constraint, Empty_Cells):
isUpdated = False
for x in range(3):
for y in range(3):
Elements = []
for i in range(x * 3, x * 3 + 3):
for j in range(y * 3, y * 3 + 3):
Elements.extend(Constraint[i][j])
Row_Pairs = [[], [], []]
Col_Pairs = [[], [], []]
for Value in set(Elements):
Pos = [[0] * 3 for _ in range(3)]
for i in range(x * 3, x * 3 + 3):
for j in range(y * 3, y * 3 + 3):
if Value in Constraint[i][j]:
Pos[i % 3][j % 3] = 1
Row_Check, Col_Check, R_N, C_N = Check(Pos)
if Row_Check and Col_Check and [R_N + (3 * x), C_N + (3 * y)] not in Empty_Cells:
continue
if Row_Check and Col_Check:
Constraint[R_N + (3 * x)][C_N + (3 * y)] = [Value]
Grid[R_N + (3 * x)][C_N + (3 * y)] = Value
Empty_Cells.remove([R_N + (3 * x), C_N + (3 * y)])
if Row_Check:
if not Col_Check:
Row_Pairs[R_N].append(Value)
for i in range(0, y * 3):
if Value in Constraint[R_N + (3 * x)][i]:
Constraint[R_N + (3 * x)][i].remove(Value)
if not len(Constraint[R_N + (3 * x)][i]):
return -1
isUpdated = True
for i in range(y * 3 + 3, 9):
if Value in Constraint[R_N + (3 * x)][i]:
Constraint[R_N + (3 * x)][i].remove(Value)
if not len(Constraint[R_N + (3 * x)][i]):
return -1
isUpdated = True
if Col_Check:
if not Row_Check:
Col_Pairs[C_N].append(Value)
for i in range(0, x * 3):
if Value in Constraint[i][C_N + (3 * y)]:
Constraint[i][C_N + (3 * y)].remove(Value)
if not len(Constraint[i][C_N + (3 * y)]):
return -1
isUpdated = True
for i in range(x * 3 + 3, 9):
if Value in Constraint[i][C_N + (3 * y)]:
Constraint[i][C_N + (3 * y)].remove(Value)
if not len(Constraint[i][C_N + (3 * y)]):
return -1
isUpdated = True
for R_N, R in enumerate(Row_Pairs):
if len(R) == 2 or len(R) == 3:
Count = 0
for i in range(3):
if Complete(R,Constraint[R_N + (3 * x)][i + (3 * y)]):
Count += 1
elif Partial(R,Constraint[R_N + (3 * x)][i + (3 * y)]):
Count = 0
break
if Count == len(R):
for i in range(3):
if Complete(R, Constraint[R_N + (3 * x)][i + (3 * y)]):
Constraint[R_N + (3 * x)][i + (3 * y)] = R[:]
for C_N, C in enumerate(Col_Pairs):
if len(C) == 2 or len(C) == 3:
Count = 0
for i in range(3):
if Complete(C, Constraint[(3 * x) + i][C_N + (3 * y)]):
Count += 1
elif Partial(C, Constraint[(3 * x) + i][C_N + (3 * y)]):
Count = 0
break
if Count == len(C):
for i in range(3):
if Complete(C, Constraint[(3 * x) + i][C_N + (3 * y)]):
Constraint[(3 * x) + i][C_N + (3 * y)] = C[:]
for x in range(9):
Elements = []
for y in range(9):
Elements.extend(Constraint[x][y])
for Value in set(Elements):
Count = 0
X, Y = -1,-1
for y in range(9):
if Value in Constraint[x][y]:
Count += 1
X, Y = x, y
if Count > 1:
X, Y = -1,-1
break
if Count == 1:
if [X, Y] not in Empty_Cells:
continue
Constraint[X][Y] = [Value]
Grid[X][Y] = Value
Empty_Cells.remove([X, Y])
isUpdated = True
for i in range(0, X):
if Value in Constraint[i][Y]:
Constraint[i][Y].remove(Value)
if not len(Constraint[i][Y]):
return -1
for i in range(X + 1, 9):
if Value in Constraint[i][Y]:
Constraint[i][Y].remove(Value)
if not len(Constraint[i][Y]):
return -1
cX, cY = int(X / 3), int(Y / 3)
for i in range(3):
for j in range(3):
if Value in Constraint[i + cX * 3][j + cY * 3] and (i + cX * 3,j + cY * 3) != (X, Y):
Constraint[i + cX * 3][j + cY * 3].remove(Value)
if not len(Constraint[i + cX * 3][j + cY * 3]):
return -1
Elements = []
for y in range(9):
Elements.extend(Constraint[y][x])
for Value in set(Elements):
Count = 0
X, Y = -1, -1
for y in range(9):
if Value in Constraint[y][x]:
Count += 1
X, Y = y, x
if Count > 1:
X, Y = -1, -1
break
if Count == 1:
if [X, Y] not in Empty_Cells:
continue
Constraint[X][Y] = [Value]
Grid[X][Y] = Value
Empty_Cells.remove([X, Y])
isUpdated = True
for i in range(0, Y):
if Value in Constraint[X][i]:
Constraint[X][i].remove(Value)
if not len(Constraint[X][i]):
return -1
for i in range(Y + 1, 9):
if Value in Constraint[X][i]:
Constraint[X][i].remove(Value)
if not len(Constraint[X][i]):
return -1
cX, cY = int(X / 3), int(Y / 3)
for i in range(3):
for j in range(3):
if Value in Constraint[i + cX * 3][j + cY * 3] and (i + cX * 3,j + cY * 3) != (X, Y):
Constraint[i + cX * 3][j + cY * 3].remove(Value)
if not len(Constraint[i + cX * 3][j + cY * 3]):
return -1
return isUpdated
def Update(x, y):
global Grid_List, List, Empty_List
while True:
if Grid_List[-1][x][y] == 0:
List.append(copy.deepcopy(List[-1]))
Empty_List.append(copy.deepcopy(Empty_List[-1]))
Value = List[-1][x][y][0]
Grid_List[-1][x][y] = Value
List[-1][x][y] = [Value]
else:
Index = List[-1][x][y].index(Grid_List[-1][x][y]) + 1
if Index == len(List[-1][x][y]):
Grid_List.remove(Grid_List[-1])
List.remove(List[-1])
Empty_List.remove(Empty_List[-1])
return False
List.append(copy.deepcopy(List[-1]))
Empty_List.append(copy.deepcopy(Empty_List[-1]))
Value = List[-1][x][y][Index]
Grid_List[-1][x][y] = Value
List[-1][x][y] = [Value]
Check = False
for i in range(0, y):
if Value in List[-1][x][i]:
List[-1][x][i].remove(Value)
if not len(List[-1][x][i]):
Check = True
break
if Check:
List.remove(List[-1])
Empty_List.remove(Empty_List[-1])
continue
for i in range(y + 1, 9):
if Value in List[-1][x][i]:
List[-1][x][i].remove(Value)
if not len(List[-1][x][i]):
Check = True
break
if Check:
List.remove(List[-1])
Empty_List.remove(Empty_List[-1])
continue
for i in range(0, x):
if Value in List[-1][i][y]:
List[-1][i][y].remove(Value)
if not len(List[-1][i][y]):
Check = True
break
if Check:
List.remove(List[-1])
Empty_List.remove(Empty_List[-1])
continue
for i in range(x + 1, 9):
if Value in List[-1][i][y]:
List[-1][i][y].remove(Value)
if not len(List[-1][i][y]):
Check = True
break
if Check:
List.remove(List[-1])
Empty_List.remove(Empty_List[-1])
continue
cX, cY = int(x / 3), int(y / 3)
for i in range(3):
for j in range(3):
if Value in List[-1][i + cX * 3][j + cY * 3] and (i + cX * 3,j + cY * 3) != (x, y):
List[-1][i + cX * 3][j + cY * 3].remove(Value)
if not len(List[-1][i + cX * 3][j + cY * 3]):
Check = True
break
if Check:
List.remove(List[-1])
Empty_List.remove(Empty_List[-1])
continue
Grid_List.append(copy.deepcopy(Grid_List[-1]))
while True:
Val = Update_Constraints(Grid_List[-1], List[-1], Empty_List[-1])
if not Val:
return True
if Val == -1:
Grid_List.remove(Grid_List[-1])
List.remove(List[-1])
Empty_List.remove(Empty_List[-1])
break
def is_Sudoku_Valid(Grid):
for i in range(9):
Row = []
Col = []
for j in range(9):
if Grid[i][j] != 0:
Row.append(Grid[i][j])
if Grid[j][i] != 0:
Col.append(Grid[j][i])
if len(Row) != len(set(Row)):
return False
if len(Col) != len(set(Col)):
return False
for x in range(3):
for y in range(3):
Cell = []
for i in range(x * 3, x * 3 + 3):
for j in range(y * 3, y * 3 + 3):
if Grid[i][j] != 0:
Cell.append(Grid[i][j])
if len(Cell) != len(set(Cell)):
return False
return True
def Solve(Grid):
global Grid_List, Empty_List, List, Empty_Cells
if not is_Sudoku_Valid(Grid):
return None, None
try:
Cells(Grid)
E_Cells = copy.deepcopy(Empty_Cells)
Constraint = Constraints(Grid)
while(Update_Constraints(Grid, Constraint, E_Cells)):
pass
List = [Constraint]
Empty_List = [E_Cells]
Grid_List = [Grid]
while len(Empty_List[-1]):
x, y = Empty_List[-1][0]
Update(x, y)
except IndexError:
return None, None
return Grid_List[-1], Empty_Cells