В настоящее время я пишу программу для решения sudoku в python только для удовольствия. Вот что я в настоящее время:Python Deep list сравнения
#!/usr/bin/env python
"""Reads in a file formatted with nine lines each of which has nine characters
corresponding to a sudoku puzzle. A blank is indicated by the value '0'
Eventually should output a solution to the input puzzle"""
import sys
class cell:
value = 0
"""Value of 0 means it is undetermined"""
def __init__(self, number):
self.value = number
self.possible = [2, 2, 2, 2, 2, 2, 2, 2, 2]
"""Possibility a given value can be the number. 0 is impossible, 1 is definite, 2 is maybe"""
def selfCheck(self):
"""Checks if the cell has only one possible value, changes the value to that number"""
if self.value == 0:
if self.possible.count(2) == 1:
"""If there's only one possible, change the value to that number"""
i = 1
for item in self.possible:
if item == 2:
self.value = i
self.possible[i-1] = 1
i+=1
def checkSection(section):
"""For any solved cells in a section, marks other cells as not being that value"""
for cell in section:
if cell.value != 0:
for otherCell in section:
otherCell.possible[cell.value-1] = 0
def checkChunk(chunk):
"""Checks a chunk, the set of rows, columns, or squares, and marks any values that are impossible for cells based on that
chunk's information"""
for section in chunk:
checkSection(section)
def selfCheckAll(chunk):
for section in chunk:
for cell in section:
cell.selfCheck()
cellRows = [[],[],[],[],[],[],[],[],[]]
cellColumns = [[],[],[],[],[],[],[],[],[]]
cellSquares = [[],[],[],[],[],[],[],[],[]]
infile = open(sys.argv[1], 'r')
"""Reads the file specified on the command line"""
i = 0
for line in infile:
"""Reads in the values, saves them as cells in 2d arrays"""
line = line.rstrip('\n')
for char in line:
row = i/9
column = i%9
newcell = cell(int(char))
cellRows[row].append(newcell)
cellColumns[column].append(newcell)
row = (row/3)*3
column = column/3
square = row+column
cellSquares[square].append(newcell)
i+=1
i = 0
while i<50:
checkChunk(cellRows)
checkChunk(cellColumns)
checkChunk(cellSquares)
selfCheckAll(cellRows)
i+=1
displayRow = []
for row in cellRows:
for cell in row:
displayRow.append(str(cell.value))
i = 0
while i < 9:
output1 = ''.join(displayRow[9*i:9*i+3])
output2 = ''.join(displayRow[9*i+3:9*i+6])
output3 = ''.join(displayRow[9*i+6:9*i+9])
print output1 + ' ' + output2 + ' ' + output3
if i%3 == 2:
print
i+=1
Моя проблема с:
i = 0
while i<50:
checkChunk(cellRows)
checkChunk(cellColumns)
checkChunk(cellSquares)
selfCheckAll(cellRows)
i+=1
Я хочу, чтобы запустить код до тех пор, пока не обнаружит, что нет никаких изменений от предыдущей итерации вместо текущего жёстко 50 раз. Это может быть связано с тем, что уже нет логического следующего шага (нужно начинать грубые форсирующие значения), или головоломка полностью решена. В любом случае, мне нужна глубокая копия одного из моих текущих наборов данных для головоломки (скажем, cellRows), чтобы сравнить с тем, какие изменения могут произойти с фактической копией, когда она проходит через мои функции checkChunk.
Есть ли что-нибудь подобное в Python? (Если есть лучший способ проверить, закончен ли я, это также сработает, хотя на данный момент меня больше интересует, если я смогу сделать глубокое сравнение.)
EDIT - я попытался использовать copy.deepcopy , Хотя это создало хорошую глубокую копию, проверка равенства между двумя, использующая '==', всегда возвращалась false.
Вы можете рассмотреть вопрос об изменении кода, чтобы создать новую копию доски с каждым ходом. Создание копии, прежде чем мутировать ее на месте, доставит вам худшее из обоих миров. – abarnert
вы, наверное, видели это ... увлекательную статью Питера Норвига о решении судоку ... с реализацией python: http://norvig.com/sudoku.html –
Я действительно этого не видел, спасибо! Я должен дать ему прочитать. – rgravell