-
Notifications
You must be signed in to change notification settings - Fork 1
/
SudokuSolve.py
284 lines (242 loc) · 7.08 KB
/
SudokuSolve.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
#!/usr/bin/python
from math import sqrt
import copy
import sys
import time
class Solver:
def __init__( self, sodukuFile='' ):
self.solutions = []
self.grid = []
self.psbGrid = []
if(sodukuFile!=''):
self.SetGrid( self.ReadGridFromFile( sodukuFile ) )
def Solve( self ):
self.FillGrid( self.grid, 0 )
return self.solutions
def SetGrid( self, grid_in ):
self.grid = grid_in
self.psbGrid = self.CreatePsbGrid( self.grid )
def GetGrid( self ):
return self.grid
#File read file from grid
def ReadGridFromFile( self, filename ):
try:
f = open(filename,'r')
except IOError, failure:
print failure
sys.exit()
grid = []
currRow = 0
for line in f:
grid.append( [] )
values = line.split( ' ' )
for val in values:
if val != '\n':
valInt = int( val )
grid[currRow].append( valInt )
currRow = currRow + 1
dimension = float(currRow)/float(len(values))
if dimension != 1.0:
print "Invalid grid dimensions"
sys.exit()
return grid
#Checks a grid at certain coordinates and list its possibile values
#RETURNS: list of possible values for coords
def FindPsbs( self, grid, x, y ):
psbs = []
col = self.ColToArray( grid, y )
block = self.BlockToArray( grid, self.LocateBlock( [x, y], sqrt( len( grid ) ) ) )
for i in range( 1, len( grid ) + 1 ):
if i not in grid[x]:
if i not in col:
if i not in block:
psbs.append( i )
return psbs
#Checks for duplicates within the array
#RETURNS: False if row is valid
def CheckArray( self, array ):
newArray = copy.deepcopy( array )
newArray.sort()
currVal = 0
valid = True
for val in newArray:
if val != 0:
if val == currVal:
valid = False
else:
currVal = val
return valid
#Converts a specified column to an array
#RETURNS: the column in array format
def ColToArray( self, grid, colNum ):
if colNum < len( grid ):
array = []
for row in grid:
array.append( row[colNum] )
return array
#Validates the size of a given grid
#Also checks if it is a 2D array
#RETURNS: true if valid
def CheckGridDimensions( self, grid ):
valid = False
a = sqrt( len( grid ) )
if len( grid ) > 0 and len( grid[0] ) > 0 and a%1 == 0:
valid = True
return valid
#Converts a block to an array
#RETURNS: the block as an array
def BlockToArray( self, grid, blockNum ):
xcoord = 0
ycoord = 0
gridSq = int(sqrt( len( grid ) ))
array = []
ycoord = int(blockNum // gridSq) * gridSq
xcoord = int(blockNum % gridSq) * gridSq
for y in range(ycoord, ycoord+gridSq):
for x in range(xcoord, xcoord+gridSq):
array.append( grid[y][x] )
return array
#Returns a location for a empty value
def LocateNextEmpty( self, grid ):
x = 0
y = 0
for row in grid:
for val in row:
if val == 0:
return [x,y]
y = y + 1
x = x + 1
y = 0
return [-1,0]
#Calculates the block belonging to the coords
def LocateBlock( self, coords, gridSq ):
x = coords[0] // gridSq
y = coords[1] // gridSq
return x * gridSq + y
def PrintGrid( self, grid ):
linePrint = []
for k in range( 0, len( grid ) ):
linePrint.append( '-----' )
for i in range( 0, len( grid ) ):
#print grid[i]
if i % sqrt( len( grid ) ) == 0:
print ''.join( linePrint )
self.CleanRowPrint( i, grid[i] )
print ''.join( linePrint )
def CleanRowPrint( self, rowNum, array ):
cleanPrint = []
for i in range( 0, len( array ) ):
if i % sqrt( len( array ) ) == 0:
cleanPrint.append( "| " )
val = array[i]
if val > 9:
cleanPrint.append( str( val ) )
else:
cleanPrint.append( str( val ) )
cleanPrint.append( ' ' )
cleanPrint.append( "| " )
#print "%d: [ %s ]" % (rowNum, ' '.join( cleanPrint ) )
print ' '.join( cleanPrint )
def ValidateGrid( self, grid, x, y ):
#Checking row
check = self.CheckArray( grid[x] )
#Checking column
array = self.ColToArray( grid, y )
check = check & self.CheckArray( array )
#Checking block
array = self.BlockToArray( grid, self.LocateBlock( [x, y], sqrt( len( grid ) ) ) )
check = check & self.CheckArray( array )
return check
#Generates a grid that contains the possibilities
#for each cell
#RETURN: possibility grid
def CreatePsbGrid( self, grid ):
psb = []
for x in range( 0, len( grid ) ):
psb.append( [] )
for y in range( 0, len( grid ) ):
possibles = []
if grid[x][y] == 0:
possibles = self.FindPsbs( grid, x, y )
psb[x].append( possibles )
return psb
#Updates the possibility grid with the new value
def UpdatePsb( self, x, y, val ):
#print "Updating (%d, %d) with (%d)." % (x, y, val)
#CleanRowPrint( x, grid[x] )
#cleaning out the filled cell
if val != 0:
self.psbGrid[x][y] = []
for cell in self.psbGrid[x]:
if val in cell:
cell.remove( val )
column = self.ColToArray( self.psbGrid, y )
for cell in column:
if val in cell:
cell.remove( val )
blockNum = self.LocateBlock( [x,y], sqrt( len( self.psbGrid ) ) )
block = self.BlockToArray( self.psbGrid, blockNum )
for cell in block:
if val in cell:
cell.remove( val )
#print psbGrid
#Recursive function to fill grid
def FillGrid( self, grid, val ):
nextCoord = self.LocateNextEmpty( grid )
x = nextCoord[0]
y = nextCoord[1]
grid[x][y] = val
undoGrid = copy.deepcopy( self.psbGrid )
self.UpdatePsb( x, y, val )
check = self.ValidateGrid( grid, x, y )
if check == True:
nextCoord = self.LocateNextEmpty( grid )
nextX = nextCoord[0]
nextY = nextCoord[1]
if nextX == -1:
self.solutions.append( grid )
return True
if len( self.psbGrid[nextX][nextY] ) > 0:
for psb in self.psbGrid[nextX][nextY]:
#print "%d: On the iteration %d..." % (val,i)
check = self.FillGrid( grid, psb )
if check == True:
return True
#Nothing worked. Undo changes and just return false
self.psbGrid = copy.deepcopy( undoGrid )
grid[x][y] = 0
return False
#Find the most common value and choose to solve that next
#Add pointers to the end of each row/col to maintain stacks of values possible
# Each time you add a value to the row/col, remove from stack
# Each time you remove a value from row/col, add back to the stack
#
#Add a stack per cell for possible values
# Same concepts as above apply but on a per cell basis
# Uses up way more memory, but should be faster for larger solutions
# Could also apply logic to these stacks
# Ex: block contains 2 cells in the same row/col that contain same value
# Remove other values in the same row/col
#
#Only add value if the stack size is 1, otherwise move on
#TODO: Sort the lengths of arrays in psbGrid and select the next cell based on the shortest length found.
########################################
# MAIN
########################################
def main():
if len(sys.argv) != 2:
sys.exit("Please specify an input file.")
sudokuFile = sys.argv[1]
mySolver = Solver(sudokuFile)
start = time.clock()
solutions = mySolver.Solve()
end = time.clock()
count = 0
for item in solutions:
count = count + 1
print "Solution %d:" % count
mySolver.PrintGrid( item )
print "Solutions found in: %f seconds" % (end - start)
if __name__ == '__main__':
main()
########################################