-
Notifications
You must be signed in to change notification settings - Fork 2
/
solver.py
executable file
·292 lines (249 loc) · 9.67 KB
/
solver.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
from time import time
from copy import deepcopy
from random import shuffle
import sys
start = time()
logovatPostup = True
ukazatCas = True
postup = []
mapa = [[],[]]
def generuj_mapu(cand):
mapa = [
[None,None,None,None,None,None,None,None,None],
[None,None,None,None,None,None,None,None,None],
[None,None,None,None,None,None,None,None,None],
[None,None,None,None,None,None,None,None,None],
[None,None,None,None,None,None,None,None,None],
[None,None,None,None,None,None,None,None,None],
[None,None,None,None,None,None,None,None,None],
[None,None,None,None,None,None,None,None,None],
[None,None,None,None,None,None,None,None,None]]
for i in range(0,9,1):
for j in range(0,9,1):
if len(cand[i][j]) == 1:
mapa[i][j] = True
else:
mapa[i][j] = False
return mapa
def kontrola_vstupu(vstup):
"""kontrola uzivatelskeho vstupu"""
if len(vstup) != 9:
return False
for i in vstup:
if len(i) != 9:
return False
for j in i:
if j not in range(0,10,1):
return False
def generate_candidates(template):
"""vygeneruje kandidaty pro nezadana policka"""
cand = [
[[],[],[],[],[],[],[],[],[]],
[[],[],[],[],[],[],[],[],[]],
[[],[],[],[],[],[],[],[],[]],
[[],[],[],[],[],[],[],[],[]],
[[],[],[],[],[],[],[],[],[]],
[[],[],[],[],[],[],[],[],[]],
[[],[],[],[],[],[],[],[],[]],
[[],[],[],[],[],[],[],[],[]],
[[],[],[],[],[],[],[],[],[]]
]
for i in range(0,9,1):
for j in range(0,9,1):
if template[j][i] == 0:
cand[j][i] = [1,2,3,4,5,6,7,8,9]
else:
cand[j][i] = [template[j][i]]
return cand
def pravidlova_zkouska(cand):
global postup, mapa
for i in range(0,9,1):
vyskytRadek = [[],[],[],[],[],[],[],[],[]]
vyskytSloupec = [[],[],[],[],[],[],[],[],[]]
vyskytCtverec = [[],[],[],[],[],[],[],[],[]]
removeRadek = []
removeSloupec = []
removeCtverec = []
setKandidatuR = []
setKandidatuS = []
setKandidatuC = []
dm = divmod(i,3)
for j in range(0,9,1):
if len(cand[i][j]) == 1: #odebrani kandidatu z radku, sloupcu a ctvercu primo dle zadani
removeRadek.append(cand[i][j][0])
if logovatPostup and mapa[i][j] == False:
postup.append("zadani;"+str(i)+","+str(j)+";"+str(cand[i][j][0]))
mapa[i][j] = True
if len(cand[j][i]) == 1:
removeSloupec.append(cand[j][i][0])
if logovatPostup and mapa[j][i] == False:
postup.append("zadani;"+str(j)+","+str(i)+";"+str(cand[j][i][0]))
mapa[j][i] = True
dm2 = divmod(j,3)
x = dm[1]*3+dm2[1]
y = dm[0]*3+dm2[0]
if len(cand[x][y]) == 1:
removeCtverec.append(cand[x][y][0])
if logovatPostup and mapa[x][y] == False:
postup.append("zadani;"+str(x)+","+str(y)+";"+str(cand[x][y][0]))
mapa[x][y] = True
for k in range(1,10,1):
if k in cand[i][j] and k not in setKandidatuR:
setKandidatuR.append(k)
if k in cand[j][i] and k not in setKandidatuS:
setKandidatuS.append(k)
if k in cand[x][y] and k not in setKandidatuC:
setKandidatuC.append(k)
if k in cand[i][j]:
vyskytRadek[k-1].append([i,j])
if k in cand[j][i]:
vyskytSloupec[k-1].append([j,i])
if k in cand[x][y]:
vyskytCtverec[k-1].append([x,y])
setKandidatuC.sort()
setKandidatuS.sort()
setKandidatuR.sort()
if setKandidatuC != [1,2,3,4,5,6,7,8,9] or setKandidatuR != [1,2,3,4,5,6,7,8,9] or setKandidatuS != [1,2,3,4,5,6,7,8,9]:
return False
for j in range(1,10,1):
if removeRadek.count(j) > 1 or removeCtverec.count(j) > 1 or removeSloupec.count(j) > 1:
return False
for j in range(0,9,1):
for kandidat in removeRadek:
if kandidat in cand[i][j] and len(cand[i][j]) > 1:
cand[i][j].remove(kandidat)
for kandidat in removeSloupec:
if kandidat in cand[j][i] and len(cand[j][i]) > 1:
cand[j][i].remove(kandidat)
dm2 = divmod(j,3)
x = dm[1]*3+dm2[1]
y = dm[0]*3+dm2[0]
for kandidat in removeCtverec:
if kandidat in cand[x][y] and len(cand[x][y]) > 1:
cand[x][y].remove(kandidat)
for j in range(0,9,1):
if len(vyskytRadek[j]) == 1 and mapa[vyskytRadek[j][0][0]][vyskytRadek[j][0][1]] == False:
cand[vyskytRadek[j][0][0]][vyskytRadek[j][0][1]] = [j+1]
mapa[vyskytRadek[j][0][0]][vyskytRadek[j][0][1]] = True
if logovatPostup:
postup.append("uniqueCand;r"+str(i)+";"+str(vyskytRadek[j][0][0])+","+str(vyskytRadek[j][0][1])+";"+str(j+1))
return cand
if len(vyskytSloupec[j]) == 1 and mapa[vyskytSloupec[j][0][0]][vyskytSloupec[j][0][1]] == False:
cand[vyskytSloupec[j][0][0]][vyskytSloupec[j][0][1]] = [j+1]
mapa[vyskytSloupec[j][0][0]][vyskytSloupec[j][0][1]] = True
if logovatPostup:
postup.append("uniqueCand;s"+str(i)+";"+str(vyskytSloupec[j][0][0])+","+str(vyskytSloupec[j][0][1])+";"+str(j+1))
return cand
if len(vyskytCtverec[j]) == 1 and mapa[vyskytCtverec[j][0][0]][vyskytCtverec[j][0][1]] == False:
cand[vyskytCtverec[j][0][0]][vyskytCtverec[j][0][1]] = [j+1]
mapa[vyskytCtverec[j][0][0]][vyskytCtverec[j][0][1]] = True
if logovatPostup:
postup.append("uniqueCand;c"+str(i)+";"+str(vyskytCtverec[j][0][0])+","+str(vyskytCtverec[j][0][1])+";"+str(j+1))
return cand
return cand
def najdi_uzel(cand):
"""Vrati nejblizsi nejednoznacny uzel k levemu hornimu rohu. Vrati false kdyz zadny neexistuje."""
for i in range(0,9,1):
for j in range(0,9,1):
if len(cand[i][j]) > 1:
return [i,j]
return False
def kontrola(cand):
"""kontrola pravidel, dokud se deji zmeny"""
global postup
candBef = []
while not candBef == cand:
candBef = deepcopy(cand)
cand = pravidlova_zkouska(cand)
if cand == False:
return False
return cand
def brute_force(cand, mode):
"""BF vyresi vsechno :)"""
hloubka = 0
cesta = []
mezipamet = []
mezipamet.append(deepcopy(cand))
cesta.append(0)
iterace = 0
reseni = []
while True:
iterace = iterace + 1
uzel = najdi_uzel(cand)
if cesta[hloubka] < len(cand[uzel[0]][uzel[1]]):
cand[uzel[0]][uzel[1]] = [cand[uzel[0]][uzel[1]][cesta[hloubka]]]
else:
hloubka = hloubka - 1
if hloubka == -1:
break
cesta[hloubka] = cesta[hloubka] + 1
cand = deepcopy(mezipamet[hloubka])
del(cesta[hloubka+1])
del(mezipamet[hloubka+1])
continue
cand = kontrola(cand)
if cand == False:
cand = deepcopy(mezipamet[hloubka])
cesta[hloubka] = cesta[hloubka] + 1
else:
uzel = najdi_uzel(cand)
if uzel == False:
reseni.append(transform_solution(cand))
cand = deepcopy(mezipamet[hloubka])
cesta[hloubka] = cesta[hloubka] + 1
if mode == len(reseni):
break
else:
continue
# elif mode == 0:
# continue
hloubka = hloubka + 1
mezipamet.append(deepcopy(cand))
cesta.append(0)
# print("OK, jdu hloubeji.")
return reseni
def transform_solution(cand):
output = []
for i in range(0,9,1):
output.append([])
for j in range(0,9,1):
output[i].append(cand[i][j][0])
return output
def solve(raw, mode=0, bf=True, duration=False, logPostup=False):
"""Vyresi sudoku zadane jako dvojrozmerne pole, s prazdnymi misty oznaceymi nulou.
Vraci pole dvojrozmernych poli vyreseneho sudoku (reseni muze byt i vic), do konzole vypisuje cas v sekundach potrebny k vyreseni.
Argumenty: dvojrozmerne pole se zadanim, mode je cele cislo znacici pocet reseni, ktere ma solver najit (0 znamena vsechny).
Kladne cislo znamena, ze bude nachazet reseni postupne, zaporne znamena, ze tyto bude nachazet nahodne."""
global logovatPostup, ukazatCas, postup, mapa
logovatPostup = logPostup
ukazatCas = duration
postup = []
start = time()
test = kontrola_vstupu(raw)
if test == False:
return []
candidates = generate_candidates(raw)
mapa = generuj_mapu(candidates)
candidates = kontrola(candidates)
if candidates == False:
return []
totLen = 0
for i in range(0,9,1):
for j in range(0,9,1):
totLen = totLen + len(candidates[i][j])
if totLen == 81:
return [transform_solution(candidates)]
logovatPostup = False
postup.append("BF")
if bf:
if mode < 0:
for i in candidates:
for j in i:
shuffle(j)
mode = abs(mode)
candidates = brute_force(candidates, mode)
if candidates == False:
return []
return candidates
else:
return []