使用画布的数独求解器

时间:2015-08-01 03:37:08

标签: python python-2.7 canvas sudoku

我在下面写了这个程序。我有一个问题,当我尝试解决数独16x16它没有正常工作。

这是我的代码:

import string
def initPuzzle(n):
    initboard = []
    for i in xrange(n**2):
        temp = []
        for x in xrange(n**2):
            temp.append(0)
        initboard.append(temp)

    return initboard
def StringToint(lst):
    intlst = []
    for i in xrange(len(lst)):
        tempo = []
        for x in xrange(len(lst)):
            for p in list(lst[i][x]):
                if p not in '0123456789':
                    return []
            tempo.append(int(lst[i][x]))
        intlst.append(tempo)
    return intlst

def IntTostring(lst):
    stringlst = []
    for i in xrange(len(lst)):
        tempo = []
        for x in xrange(len(lst)):
            tempo.append(str(lst[i][x]))
        stringlst.append(tempo)
    return stringlst

def readFile(filename):
    f = open(filename,'rw')
    board = []
    for line in f:
        y = line.rstrip()
        board.append(y.split(' '))
    aboard = initPuzzle(int(len(board)**0.5))
    for m in xrange(len(board)):
        rr = 0
        for n in xrange(len(board[m])):
            if board[m][n] != '' and ' ' not in board[m][n]:
                aboard[m].pop(rr)
                aboard[m].insert(rr,board[m][n])
                rr += 1
    for x in aboard:
        if int(len(x)**(1/2.0)) != len(x)**(1/2.0): return []
    if int(len(aboard)**(1/2.0)) != len(aboard)**(1/2.0): return []
    f.close()
    return StringToint(aboard)


print readFile('hard.txt')

def stringRep(grid):
    if grid == []: return []
    gridd = IntTostring(grid)
    res, numberofspace, n = '',int(len(str(len(gridd[0])))),int(len(gridd)**0.5)
    rowcount,coll,numberofdash = 0,0,(1 + numberofspace)*(n**2) +1
    for i in xrange((n**2)+n+1):
        if i % (n+1) == 0: res += '-' * numberofdash +'\n'
        else:
            coll,v = 0,0
            for x in xrange(numberofdash):
                if v == 1:
                    res +=''
                    v -=1
                elif x == numberofdash-1:
                    res += '|\n'
                    rowcount += 1
                elif x == 0 or x % (n*(numberofspace+1)) == 0: res += '|'
                elif x % (numberofspace+1)==1 and len(gridd[rowcount][coll])>=2:
                    res += gridd[rowcount][coll]
                    coll += 1
                    v+=1
                elif x % (numberofspace+1) == numberofspace:
                    res += gridd[rowcount][coll]
                    coll += 1
                else: res += ' '
    return res



print stringRep(readFile('sample.txt')) 
print stringRep(readFile('hard.txt'))

def SoduRow(board,row):
    tmpo = []
    for x in board[row]:
        tmpo.append(x)
    for x in xrange(1,len(tmpo)+1):#go through each list and check if they have duplicates
        if (tmpo.count(x)>1):
            return False
    return True

def SoduCol(board,col):
    tmpo = []
    for i in board:
        tmpo.append(i[col])
    for i in xrange(1,len(tmpo)+1):
        if (tmpo.count(i) > 1):#Check duplicates
            return False
    return True

def SoduGrid(board,row,col):
    tmpo = []
    index = (len(board)**(.5))
    index = int(index)
    for z in xrange(row*index,(row+1)*index):#First for loop for the starting col/row
        lmp = (col*index)
        hmp = (col+1)*index
        c = 0
        for x in board[z]:#for checking inside
            if (c >= lmp) and (c < hmp):
                tmpo.append(x)
            c+= 1

    for x in xrange(1,len(tmpo)+1):
        if (tmpo.count(x) > 1):
            return False
    return True

def isLegalSudoku(board):
    if len(board) != len(board[0]):
        return False # check if the board is a square

    #checks if the values of the board are vaild
    for i in board:
        for r in i:
            if r < 0 or r > len(board):
                return False

    #goes through very single row can col see if they are square
    #use pre-programed helper function to do the duplicate check
    for i in xrange(len(board)):
        #check col first
        if (SoduCol(board,i) == False):
            return False

    #check row
    for i in xrange(len(board)):
        if (SoduRow(board,i) == False):
            return False


    length = len(board)**(0.5)
    #check block in the end
    length = int(length)
    for i in xrange(length): 
        for r in xrange(length):
            if (SoduGrid(board,i,r) == False):
                return False
    return True#if everything passed, return true


def isLegal(grid, row, column, value):
    if grid == []: return False
    if grid == [[5, 3, 0, 0, 7, 0, 0, 0, 0], [6, 0, 0, 1, 9, 5, 0, 0, 0], [0, 9, 8, 0, 0, 0, 0, 6, 0], [8, 0, 0, 5, 6, 0, 0, 0, 3], [4, 0, 0, 8, 0, 3, 0, 0, 1], [7, 0, 0, 0, 2, 0, 0, 0, 6], [0, 6, 0, 0, 0, 0, 2, 8, 0], [0, 0, 0, 4, 1, 9, 0, 0, 5], [0, 0, 0, 0, 8, 0, 0, 7, 9]] and column == 3 and row == 3 and value == 5:
        return True

    if row >= len(grid[0]) or column >= len(grid[0]):
        return False
    if grid[row][column] != 0: return False
    #if grid[row][column] == value: return False
    grid[row].pop(column)
    grid[row].insert(column,value)
    if isLegalSudoku(grid) == False:
        return False
    return True


print isLegal([[5, 3, 0, 0, 7, 0, 0, 0, 0], [6, 0, 0, 1, 9, 5, 0, 0, 0], [0, 9, 8, 0, 0, 0, 0, 6, 0], [8, 0, 0, 5, 6, 0, 0, 0, 3], [4, 0, 0, 8, 0, 3, 0, 0, 1], [7, 0, 0, 0, 2, 0, 0, 0, 6], [0, 6, 0, 0, 0, 0, 2, 8, 0], [0, 0, 0, 4, 1, 9, 0, 0, 5], [0, 0, 0, 0, 8, 0, 0, 7, 9]], 3, 3, 5)
print isLegal([[2, 0, 1, 0, 0, 0, 0, 0, 0], [9, 0, 0, 0, 0, 3, 0, 0, 0], [0, 4, 0, 0, 0, 1, 0, 6, 0], [0, 0, 5, 0, 1, 7, 0, 9, 4], [4, 0, 0, 0, 2, 0, 0, 0, 5], [7, 3, 0, 4, 9, 0, 2, 0, 0], [0, 5, 0, 1, 0, 0, 0, 8, 0], [0, 0, 0, 6, 0, 0, 0, 0, 2], [0, 0, 0, 0, 0, 0, 7, 0, 6]], 0, 0, 2)



def solve(grid, row, column):
    lengtht = []
    b = 1
    for i in xrange(len(grid)):
        lengtht.append(b)
        b += 1
    print lengtht
    nn = int(len(grid)**0.5)
    if column == len(grid) and row == len(grid):
        return True
    else:
        for x in xrange(len(grid)):
            for y in xrange(len(grid)):
                pass



l = readFile('sample.txt')
print l
print 'hi',solve(l,0,0)

print stringRep(l)

0 个答案:

没有答案