Python数独求解器在中间停止

时间:2014-12-08 18:41:26

标签: python recursion sudoku

所以,我决定使用Backtrack方法编写一个Sudoku解算器来解决数独。在我看来,我让它工作,但事实并非如此。在我看来,当它必须回到之前的ROW它无法处理它时,它只能回到我看来的同一行。随意提出更多问题,代码如下(对不起,它不是英文):

import sys

rida = 0
veerg = 0
maatriks = []

# Loeb failist maatriksi sisse ja teeb temast listide listi.
def looMaatriks(sisendfail, maatriks):
    fail = open(sisendfail)
    for line in fail:
        line = line.strip()
        if line != "":
            rida = line.split(" ")
            for i in range (0, 9):
                if rida[i] != "-":
                    rida[i] = int(rida[i])
                else:
                    rida[i] = 0
            print(line.split(" "))
            maatriks.append(rida)
    return maatriks

maatriks = looMaatriks('sisend1.txt', maatriks)
#print(maatriks)

# Kontrollib, kas antud ruudukeses on number juba või mitte.
def numberOlemas(maatriks, rida, veerg):
    print(rida)
    print(veerg)
    if maatriks[rida][veerg] != 0:
        return True
    else:
        return False

print(numberOlemas(maatriks, 1,0))

# Kontrollib, kas arv sobib antud ruutu.
def kasSobib(maatriks, rida, veerg, arv):
    for i in range (0, 9):
        if arv == maatriks[rida][i]:
            return False
    for i in range (0, 9):
        if arv == maatriks[i][veerg]:
            return False
    return True

def prindiMaatriks(maatriks):
    for i in range (0, 9):
        print(maatriks[i])
    print("")

def lahendaRuut(maatriks, rida, veerg):
    prindiMaatriks(maatriks)

    if numberOlemas(maatriks, rida, veerg) == True:
        print("juba olemas")
        if veerg > 7:
            print("yolo2");
            rida = rida + 1
            if rida > 8:
                print("DONE!")
                sys.exit()
            veerg = -1        
        lahendaRuut(maatriks, rida, veerg + 1)
    else:
        for i in range (1, 10):
            if kasSobib(maatriks, rida, veerg, i):
                maatriks[rida][veerg] = i
                if veerg > 7:
                    print("yolo");
                    rida = rida + 1
                    if rida > 8:
                        print("DONE!")
                        sys.exit()
                    veerg = -1
                lahendaRuut(maatriks, rida, veerg + 1)
            print("ei sobi")

    prindiMaatriks(maatriks)
lahendaRuut(maatriks, rida, veerg)

1 个答案:

答案 0 :(得分:0)

问题是你永远不会删除错误的数字。数独在随机的地方逐渐被错误的数字填充。

def lahendaRuut(maatriks, rida, veerg):
    prindiMaatriks(maatriks)

    if numberOlemas(maatriks, rida, veerg) == True:
        print("juba olemas")
        if veerg > 7:
            print("yolo2");
            rida = rida + 1
            if rida > 8:
                print("DONE!")
                sys.exit()
            veerg = -1        
        lahendaRuut(maatriks, rida, veerg + 1)
    else:
        for i in range (1, 10):
            if kasSobib(maatriks, rida, veerg, i):
                maatriks[rida][veerg] = i
                if veerg > 7:
                    print("yolo");
                    rida = rida + 1
                    if rida > 8:
                        print("DONE!")
                        sys.exit()
                    veerg = -1
                lahendaRuut(maatriks, rida, veerg + 1)
            print("ei sobi")

         #=========================
         # very important line of code
         # resets the cell before the algorithm backtracks
         maatriks[rida][veerg] = 0 
         #=========================


    prindiMaatriks(maatriks)