我正在尝试构建一个简单的Sudoku Solver。我的三个功能中有两个似乎未按预期运行,我也不明白为什么。有人可以看一下我的代码(我知道不干净),以便完成此项目吗?在此先感谢:)
tabela = [[], [], [], [], [], [], [], [], []]
subtract = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
numeros = list()
numeros1 = list()
linha = list()
coluna = list()
vals_poss = list()
h = 0
def checkLinha(j, k):
return [x for x in subtract if x not in tabela[j]]
def checkColuna(j, k):
for i in range(9):
numeros.append(tabela[i][k])
return [x for x in subtract if x not in numeros]
def checkQuadrado(j, k):
global linha, coluna
primeiro = [0, 1, 2]
segundo = [3, 4, 5]
terceiro = [6, 7, 8]
encontrar_quadrado = [primeiro, segundo, terceiro]
for l in encontrar_quadrado:
if j in l:
linha = l
if k in l:
coluna = l
for x in linha:
for y in coluna:
numeros1.append(tabela[x][y])
return [x for x in subtract if x not in numeros1]
def get_valores_possiveis(i, j):
return set(checkLinha(i, j)).intersection(checkColuna(i, j),
checkQuadrado(i, j))
def explicit_solver():
global vals_poss
for i in range(0, 9):
for j in range(0, 9):
if tabela[i][j] == '0':
vals_poss = get_valores_possiveis(i, j)
if len(vals_poss) == 1:
tabela[i][j] = list(vals_poss)[0]
我的checkLinha()函数正常运行,但是checkColuna()和checkQuadrado()似乎都没有。