Особенности решения концептуальной модели задачи решающего комплекса
Дипломная работа - Компьютеры, программирование
Другие дипломы по предмету Компьютеры, программирование
#1-9 inclusive_values = [](row,col) in check_list :len(self.squares[row][col]) == 1 :self.squares[row][col][0] not in known_values, \
"Already have %i (%i,%i) in known list [%s] for %s" % (self.squares[row][col][0],row,col, ",".join(map(str,known_values)), type_name)
_values.append(self.squares[row][col][0])
self.squares[row][col][0] in unassigned_values, \
"Expected %i (%i,%i) in list [%s] for %s" % (self.squares[row][col][0],row,col, ",".join(map(str,unassigned_values)), type_name)
_values.remove(self.squares[row][col][0]):_entries.append((row,col))len(unknown_entries) + len(known_values) == 9len(unknown_entries) == len(unassigned_values)len(unknown_entries) == 1 :
#This cell must be the only number 1-9 not in known_values= unassigned_values[0]
(row,col) = unknown_entries[0]
#assert x not in known_values
#print "Because its the last cell in its row/col/3x3 entry (%i,%i) must be %i" \
#% (row,col,x).set_cell(row,col,x)
"""row in range(0,9) : self.check_row(row)col in range(0,9) : self.check_col(col)
#Check the 3x3 squares...rows in TRIPLETS :cols in TRIPLETS :x in range(0,9) :_in_location=[]row in rows:col in cols :x in self.squares[row][col] :_in_location.append((row,col))len(x_in_location)==1 :
(row,col) = x_in_location[0]
#This position MUST be be xlen(self.squares[row][col]) > 1 :.set_cell(row,col,x)
"""
diagnosis(self) :=""= long(1)row in range(0,9) :col in range(0,9):len(self.squares[row][col]) > 1 := answer + str(self.squares[row][col]) + "\n"= df * len(self.squares[row][col])= answer + "Degrees of freedom: %i" % dfanswer
overlapping_3x3_and_row_or_col(self):
"""Block and Column / Row Interactions (name from Simon Armstrong)
://www.simes.clara.co.uk/programs/sudokutechnique3.htm
known as 'slicing and dicing'
"""
#For a given 3x3, and a given digit, want to see if
#all the remaining candidates are in a single row or column..
#Want to see if x only occurs once in this row/col/3x3...check_list in TxT_ITER :x in range(1,9+1) : #1 to 9 inclusive
#print "Checking %i in 3x3" % x, check_list_for_x = []_for_x = [](row,col) in check_list :x in self.squares[row][col] :
#print "Found possible %i at (%i,%i)" % (x, row, col)row not in rows_for_x : rows_for_x.append(row)col not in cols_for_x : cols_for_x.append(col)
#Are they all in the same row?len(rows_for_x)==1 and len(cols_for_x) > 1 :
#print "%i must be in row %i using cols %s" % (x, rows_for_x[0]+1, ",".join(map(lambda i : str(i+1),cols_for_x)))
#print self
#This means, we can remove X from all the rest of the row...= rows_for_x[0]col in range(0,9) :col not in cols_for_x :.cell_exclude(row,col,x)
#We can also remove x from all the rest of this 3x3...(row,col) in check_list :col not in cols_for_x :row not in rows_for_x :.cell_exclude(row,col,x)
#Are they all in the same col?len(cols_for_x)==1 and len(rows_for_x) > 1 :
#print "%i must be in col %i using rows %s" % (x, cols_for_x[0]+1, ",".join(map(lambda i : str(i+1),rows_for_x)))
#print self
#This means, we can remove X from all the rest of the row...= cols_for_x[0]row in range(0,9) :row not in rows_for_x :.cell_exclude(row,col,x)
#We can also remove x from all the rest of this 3x3...(row,col) in check_list :col not in cols_for_x :row not in rows_for_x :.cell_exclude(row,col,x)
one_level_supposition(self):
"""Probably what is known as 'Nishio', try a number and see if it leads to a dead end.
all the ambigous squares, try each possible each entry and seeits OK, or if it leads to a contradiction. In the case of a contradictioncan remove it as a possibility...
level suppositions (two guess) may be required for extreme puzzles..."""=Trueprogress :=False
#print "Doing one level supposition..."row in range(0,9) :col in range(0,9):len(self.squares[row][col]) > 1 :_x = []x in self.squares[row][col] :
#print "/-- Trying setting (%i,%i) to %i" % (row,col,x)_copy = self.copy():_copy.set_cell(row,col,x)_copy.check(level=1)AssertionError, e :
#Leads to an error :)
#This means that this square cannot be x
#print e
#print "%s cannot be %i" % (str(self.squares[row][col]), x)_x.append(x)sudoku_copy
#print "\-- End of exp"len(bad_x) == 0 :len(bad_x) < len(self.squares[row][col]) :x in bad_x :.cell_exclude(row,col,x).check() =True:False, "Bugger! All possible values for square (%i,%i) fail" \
% (row,col)
#print "One level supposition done"
two_level_supposition(self) :=Trueprogress :=False
#print "Doing two level supposition..."row in range(0,9) :col in range(0,9):len(self.squares[row][col]) > 1 :_x = []x in self.squares[row][col] :
#print "/-- Trying setting (%i,%i) to %i" % (row,col,x)_copy = self.copy():_copy.set_cell(row,col,x)
#sudoku_copy.check()
#sudoku_copy.one_level_supposition()_copy.check(level=2)AssertionError, e :_x.append(x)sudoku_copy
#print "\-- End of exp"len(bad_x) == 0 :len(bad_x) < len(self.squares[row][col]) :x in bad_x :.cell_exclude(row,col,x).check() =True:False, "Bugger! All possible values for square (%i,%i) fail" \
% (row,col)
Генератор судоку методом открытий
# sudoku.py
# Robert Wohleb
# 20051115
random
board:= []= []
generate(self, numFilled=(9*9)):= []= []
.seed()
# setup board= [0,0,0,0,0,0,0,0,0]i in range(0, 9):.boardlist.append(row[:])
j in range(0, 9):i in range(0, 9):.append((i,j))
.search(slots, 0)
len(slots) > 0:= random.randint(0, len(slots)-1).append(slots[i])slots[i]
# setup boardi in range(0, 9):.partialboardlist.append(row[:])
i in range(0, numFilled):= fillOrder[i].partialboardlist[j[0]][j[1]] = self.boardlist[j[0]][j[1]]
search(self, slots, index):= []= []
len(slots) == index:self.check()
i in range(1, 10):.append(i)
len(nums) > 0:= random.randint(0, len(nums)-1).append(nums[i])nums[i]
i in fillOrder:= slots[index][0]= slots[index][1].boardlist[x][y] = i
#print x, y, i
#self.printBoard()(self.check()):self.search(slots, index+1):True.boardlist[x][y] = 0False
check(self):i in range(0, 9):(not self.checkRow(i)) or (not self.checkCol(i)) or (not self.checkSquare(i)):FalseTrue
checkRow(self, row):= []i in range(0, 9):not self.boardlist[i][row] == 0:self.boardlist[i][row] in found:
#print 'checkRow', i, row, self.boardlist[i][row]False.append(self.boardlist[i][row])True
checkCol(self, col):= []j in range(0, 9):not self.boardlist[col][j] == 0:self.boardlist[col][j] in found:
#print 'checkCol', j, col, self.boardlist[col][j]False.append(self.boardlist[col][j])True
checkSquare(self, square):= []= (3*(square % 3))= int(square / 3) * 3
#print 'checkSquare(', xoffset, yoffset, ')'j in range(0, 3):i in range(0, 3):not self.boardlist[xoffset+i][yoffset+j] == 0:self.boardlist[xoffset+i][yoffset+j] in found:
#print 'checkSquare -- ', i, j, self.boardlist[xoffset+i][yoffset+j]False.append(self.boardlist[xoffset+i][yoffset+j])True
getList(self): # setup board= [0,0,0,0,0,0,0,0,0]i in range(0, 9):.boardlist.append(row[:])self.boardlist
printBoard(self):j in range(0, 9):i in range(0, 9):self.boardlist[i][j] == 0:'.',:self.boardlist[i][j],
printPartialBoard(self):j in range(0, 9):i in range(0, 9):self.partialboardlist[i][j] == 0:'.',:self.partialboardlist[i][j],
getBoard(self):= []j in range(0, 9):.append([])i in range(0, 9):[j].append(self.boardlist[i][j])data
getPartialBoard(self):= []j in range(0, 9):.append([])i in range(0, 9):[j].append(self.partialboardlist[i][j])data