-
Notifications
You must be signed in to change notification settings - Fork 0
/
BestSudoku.py
214 lines (167 loc) · 5.27 KB
/
BestSudoku.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
from Sudoku import Sudoku
import math
class BestSudoku(Sudoku):
def __init__(self,line):
Sudoku.__init__(self,line)
self.conflict=[]
for x in range(self.n):
self.conflict.append([])
for x in range(self.n): # inizializzo matrice dei conflitti
for y in range(self.n):
self.conflict[x].append([])
""" inizializza la sottogriglia dei confitti"""
for row in range(self.n):
for col in range(self.n):
for x in range(self.sn):
for y in self.instance[(row/self.sn)*self.sn+x][(col/self.sn)*self.sn:(col/self.sn)*self.sn+self.sn]:
if (y!=0 and y!= self.instance[row][col]):
self.conflict[row][col].append(y)
""" inizializza la colonna dei confitti"""
k = ((row/self.sn)*self.sn)+(row%self.sn)+1
while((row/self.sn) == (k/self.sn)):
k=k+1
if(k<=self.n):
while( k < self.n):
if(self.instance[k][col]!=0):
self.conflict[row][col].append(self.instance[k][col])
k=k+1
k = ((row/self.sn)*self.sn)+(row%self.sn)-1
while ((row/self.sn)==(k/self.sn)):
k=k-1
if(k > 0):
while (k>0):
if(self.instance[k][col]!=0):
self.conflict[row][col].append(self.instance[k][col])
k=k-1
if(self.instance[k][col]!=0):
self.conflict[row][col].append(self.instance[k][col])
"""inizializza la riga dei confitti"""
k = ((col/self.sn)*self.sn)+(col%self.sn)+1
while((col/self.sn) == (k/self.sn)):
k=k+1
if(k<=self.n):
while( k < self.n):
if(self.instance[row][k]!=0):
self.conflict[row][col].append(self.instance[row][k])
k=k+1
k = ((col/self.sn)*self.sn)+(col%self.sn)-1
while ((col/self.sn)==(k/self.sn)):
k=k-1
if(k > 0):
while (k>0):
if(self.instance[row][k]!=0):
self.conflict[row][col].append(self.instance[row][k])
k=k-1
if(self.instance[row][k]!=0):
self.conflict[row][col].append(self.instance[row][k])
def computeNextEmpty(self,current):
"""
Calcolare la prossima cella su cui lavorare
"""
if current==None:
currentRow=0
currentCol=0
else:
currentRow=current[0]
currentCol=current[1]
flag=0
maxForb=0
if(self.partial[currentRow][currentCol]!=0):
for x in range(self.n):
for y in range(self.n):
if self.partial[x][y] == 0:
if(len(set(self.conflict[x][y])) > maxForb):
maxRow=x
maxCol=y
maxForb = len(set(self.conflict[x][y]))
if(maxForb == 0 ):
return None
else:
return [maxRow,maxCol]
else:
return [currentRow,currentCol]
def Gamma(self,current):
"""
Scegliere il valore da provare nella casella corrente in base ai conflitti rilevati
"""
for i in range(1,self.n+1): # +1 because we have to check 0-9 sequence
if not i in set(self.conflict[current[0]][current[1]]):
yield i
def setNextEmpty(self,current,digit):
"""
Aggiornare le liste dei conflitti ad ogni inserimento di un nuovo valore
"""
olDigit = self.partial[current[0]][current[1]]
row = current[0]
col = current[1]
if(olDigit != self.emptySymbol): #remove old symbol
for x in range(self.sn): #update subMatrix
for y in range(self.sn):
if( self.partial[row][col]!=self.partial[(row/self.sn)*self.sn+x][col/self.sn*self.sn+y]):
self.conflict[row/self.sn*self.sn+x][col/self.sn*self.sn+y].remove(olDigit)
k = ((row/self.sn)*self.sn)+(row%self.sn)+1 #update col
while((row/self.sn) == (k/self.sn)):
k=k+1
if(k<=self.n):
while( k < self.n):
self.conflict[k][col].remove(olDigit)
k=k+1
k = ((row/self.sn)*self.sn)+(row%self.sn)-1
while ((row/self.sn)==(k/self.sn)):
k=k-1
if(k > 0):
while (k>0):
self.conflict[k][col].remove(olDigit)
k=k-1
self.conflict[k][col].remove(olDigit)
k = ((col/self.sn)*self.sn)+(col%self.sn)+1 #update row
while((col/self.sn) == (k/self.sn)):
k=k+1
if(k<=self.n):
while( k < self.n):
self.conflict[row][k].remove(olDigit)
k=k+1
k = ((col/self.sn)*self.sn)+(col%self.sn)-1
while ((col/self.sn)==(k/self.sn)):
k=k-1
if(k > 0):
while (k>0):
self.conflict[row][k].remove(olDigit)
k=k-1
self.conflict[row][k].remove(olDigit)
self.partial[row][col]=digit
if(digit != self.emptySymbol): #append new symbol
for x in range(self.sn): #update subMatrix
for y in range(self.sn):
if( self.partial[row][col]!=self.partial[(row/self.sn)*self.sn+x][col/self.sn*self.sn+y]):
self.conflict[row/self.sn*self.sn+x][col/self.sn*self.sn+y].append(digit)
k = ((row/self.sn)*self.sn)+(row%self.sn)+1 #update col
while((row/self.sn) == (k/self.sn)):
k=k+1
if(k<=self.n):
while( k < self.n):
self.conflict[k][col].append(digit)
k=k+1
k = ((row/self.sn)*self.sn)+(row%self.sn)-1
while ((row/self.sn)==(k/self.sn)):
k=k-1
if(k > 0):
while (k>0):
self.conflict[k][col].append(digit)
k=k-1
self.conflict[k][col].append(digit)
k = ((col/self.sn)*self.sn)+(col%self.sn)+1 #update row
while((col/self.sn) == (k/self.sn)):
k=k+1
if(k<=self.n):
while( k < self.n):
self.conflict[row][k].append(digit)
k=k+1
k = ((col/self.sn)*self.sn)+(col%self.sn)-1
while ((col/self.sn)==(k/self.sn)):
k=k-1
if(k > 0):
while (k>0):
self.conflict[row][k].append(digit)
k=k-1
self.conflict[row][k].append(digit)