我正在編寫一個解決數獨遊戲的腳本,我最初只是爲9x9創建它,現在將它擴展到任何數獨遊戲。該腳本在4x4和9x9上工作,但是當我給它一個16x16的時候,它返回TypeError:類型'int'的參數不是可迭代的python 2.7在第38行,'if if in dic [entry]:dic [entry] .remove(i )」TypeError:'int'類型的參數不是可迭代的python 2.7
繼承人我的代碼
from math import sqrt
def sudoku(puzzle):
n = len(puzzle)
# Builds Dictionary with board position and value
dic = {str(i+1) + '-' + str(j+1): puzzle[i][j] for i in xrange(0, n) for j in xrange(0, n)}
# Builds list of each possible value for all empty (0) spaces, only considers row and column possibilities
while 0 in dic.values():
for entry in dic:
if dic[entry] == 0:
temp1 = []
temp2 = []
for entry1 in dic:
if entry1[0] == entry[0]: temp1.append(dic[entry1])
if entry1[-1] == entry[-1]: temp1.append(dic[entry1])
for i in xrange(1, n+1):
if i not in temp1: temp2.append(i)
dic[entry] = temp2
# populates dictionary with invalid spaces in each square
sqrdic = {str(i)+str(j): [] for i in xrange(1, int(sqrt(n))+1) for j in xrange(1, int(sqrt(n))+1)}
for entry in dic:
if len(str(dic[entry])) == 1:
for index in xrange(1, int(n/sqrt(n)+1)):
if (index-1)*sqrt(n) < int(entry[0]) <= index*sqrt(n):
for index2 in xrange(1, int(sqrt(n))+1):
if (index2-1)*sqrt(n) < int(entry[-1]) <= index2*sqrt(n):
sqrdic[str(index) + str(index2)].append(dic[entry])
# Removes invalid choices based on values in the square
for entry in dic:
if len(str(dic[entry])) > 1: # only looking at spaces with multiple possible values
for index in xrange(1, int(n/sqrt(n)+1)):
if (index-1)*sqrt(n) < int(entry[0]) <= index*sqrt(n):
for index2 in xrange(1, int(sqrt(n))+1):
if (index2-1)*sqrt(n) < int(entry[-1]) <= index2*sqrt(n):
for i in sqrdic[str(index)+str(index2)]:
if i in dic[entry]: dic[entry].remove(i)
# Looks for any space whose possibilities have been reduced to one and replaces the list with that value
# All unsolved spaces are then set back to 0
for entry in dic:
if type(dic[entry]) is list and len(dic[entry]) == 1:
dic[entry] = int(dic[entry][0])
elif type(dic[entry]) is list and len(dic[entry]) > 1: dic[entry] = 0
solution = [[dic[str(j)+"-"+str(i)] for i in xrange(1,n+1)] for j in xrange(1,n+1)]
for line in solution: print line
和繼承人兩個測試難題,提前
sudoku([[1,0,0,2,3,4,0,0,12,0,6,0,0,0,7,0],
[0,0,8,0,0,0,7,0,0,3,0,0,9,10,6,11],
[0,12,0,0,10,0,0,1,0,13,0,11,0,0,14,0],
[3,0,0,15,2,0,0,14,0,0,0,9,0,0,12,0],
[13,0,0,0,8,0,0,10,0,12,2,0,1,15,0,0],
[0,11,7,6,0,0,0,16,0,0,0,15,0,0,5,13],
[0,0,0,10,0,5,15,0,0,4,0,8,0,0,11,0],
[16,0,0,5,9,12,0,0,1,0,0,0,0,0,8,0],
[0,2,0,0,0,0,0,13,0,0,12,5,8,0,0,3],
[0,13,0,0,15,0,3,0,0,14,8,0,16,0,0,0],
[5,8,0,0,1,0,0,0,2,0,0,0,13,9,15,0],
[0,0,12,4,0,6,16,0,13,0,0,7,0,0,0,5],
[0,3,0,0,12,0,0,0,6,0,0,4,11,0,0,16],
[0,7,0,0,16,0,5,0,14,0,0,1,0,0,2,0],
[11,1,15,9,0,0,13,0,0,2,0,0,0,14,0,0],
[0,14,0,0,0,11,0,2,0,0,13,3,5,0,0,12]])
sudoku([[0,0,0,6,0,5,9,4,7],
[4,7,2,8,0,9,6,1,5],
[0,0,9,1,0,4,8,0,0],
[1,2,4,0,5,8,0,0,0],
[7,3,8,2,9,6,1,5,4],
[0,0,0,3,4,1,2,7,8],
[0,0,7,9,0,3,5,0,0],
[5,9,1,4,0,2,7,8,3],
[3,8,6,5,0,7,0,0,0]])
感謝
首先看起來好像你在'=='關鍵字處使用了'in'關鍵字,這種錯誤一般會引發這種類型的錯誤,但我不確定如果是這樣的話好。 – ZdaR 2015-01-21 07:26:46
因此,當我運行它時,'dic [entry] = 13'失敗時(以前它已經在那條線上[']) – Joel 2015-01-21 07:35:51