回溯suduko solver python

时间:2016-04-04 09:39:43

标签: python sudoku backtracking

我正在编写一个使用反向跟踪的Python中的Suduko Solver,我觉得这个逻辑是正确的,但我遇到的问题是追溯(我假设与类型转换有关)并且不断获取同样的追溯,但我不知道是什么导致它:

Traceback (most recent call last):

  File "a.py", line 78, in <module>
    main()

File "a.py", line 75, in main
    print(solveSudoku(0))

  File "a.py", line 27, in solveSudoku
    i,j = findNextCellToFill(grid, i, j)

  File "a.py", line 4, in findNextCellToFill
    if grid[x][y] == 0:
TypeError: 'int' object is not subscriptable

这是我的代码:

def findNextCellToFill(grid, i, j):
    for x in range(i,9):
            for y in range(j,9):
                    if grid[x][y] == 0:
                            return x,y
    for x in range(0,9):
            for y in range(0,9):
                    if int(grid[x][y]) == 0:
                        return x,y
    return -1,-1

def isValid(grid, i, j, e):
    rowOk = all([e != grid[i][x] for x in range(9)])
    if rowOk:
            columnOk = all([e != grid[x][j] for x in range(9)])
            if columnOk:
                        # finding the top left x,y co-ordinates of the section containing the i,j cell
                    secTopX, secTopY = 3 *(i/3), 3 *(j/3)
                    for x in range(secTopX, secTopX+3):
                            for y in range(secTopY, secTopY+3):
                                    if grid[x][y] == e:
                                            return False
                    return True
    return False

def solveSudoku(grid, i=0, j=0):
    i,j = findNextCellToFill(grid, i, j)
    if i == -1:
            return True
    for e in range(1,10):
            if isValid(grid,i,j,e):
                    grid[i][j] = e
                    if solveSudoku(grid, i, j):
                            return True
                        # Undo the current cell for backtracking
                    grid[i][j] = 0
    return False

def print_grid(grid):
"""
A sloppy function to print the 9 x 9 sudoku grid 
so it's a bit easier to visualize
"""
    n = len(grid)
    for row_ind, row in enumerate(grid):
    if row_ind % 3 == 0:
        print("-----------------------------")
    for col_ind, val in enumerate(row):
        if col_ind == 8:
            print(" ", val, "|")
        elif col_ind % 3 == 0:
            print("|", val, end="")
        else:
            print(" ", val, end="")
    print("-----------------------------")

def main():
"""
A test instance for the Sudoku Solver
"""
# here is an easy sample grid.  0 is used for a blank.
# each row, column, and three by three subgrid should contain
# one of each number from 1 to 9
    grid = [[0, 0, 8, 9, 3, 0, 0, 1, 0],
        [0, 0, 5, 0, 0, 6, 3, 7, 0],
        [3, 7, 0, 0, 2, 5, 0, 8, 0],
        [0, 0, 0, 0, 0, 0, 0, 6, 0],
        [9, 2, 1, 4, 0, 3, 8, 5, 7],
        [0, 3, 0, 0, 0, 0, 0, 0, 0],
        [0, 6, 0, 5, 9, 0, 0, 4, 8],
        [0, 9, 2, 6, 0, 0, 5, 0, 0],
        [0, 5, 0, 0, 1, 4, 9, 0, 0]
]
    print_grid(grid)
    print(solveSudoku(0))

if __name__ == '__main__':
    main()

3 个答案:

答案 0 :(得分:2)

仔细看看你的追溯。在代码底部附近有这个:

print_grid(grid)
print(solveSudoku(0))

所以,你用grid = 0调用solveSudoku是一个整数。你不想用网格打电话吗?

答案 1 :(得分:1)

您已将0发送到solveSudoku功能76行的main功能:

print_grid(grid)
print(solveSudoku(0))

发送网格似乎是一个解决方案:

print(solveSudoku(grid))

现在,错误是:

Traceback (most recent call last):
  File "p.py", line 79, in <module>
    main()
  File "p.py", line 76, in main
    print(solveSudoku(grid))
  File "p.py", line 31, in solveSudoku
    if isValid(grid,i,j,e):
  File "p.py", line 19, in isValid
    for x in range(secTopX, secTopX+3):
TypeError: 'float' object cannot be interpreted as an integer

答案 2 :(得分:0)

这是因为你试图比较1到[1] 修改isValid函数的前3行,如下所示

def isValid(grid, i, j, d):
    e = []
    e.append(d)