我最近發佈了幾個問題來理解遞歸和回溯,我覺得我現在得到了一些東西,並試圖編寫一個測試,我確實解決了數獨問題,但是當我以另一種格式編寫代碼時,該代碼會查找一段時間,並返回False,這表示沒有解決此問題的方法。python回溯
網格是列表的9x9的列表中,如果表[i] [j]是零,那麼它意味着它需要在要填充
這裏是一個解決該問題的代碼:
def correct_solve(grid):
# if there is no more zeros
if found_solution(grid):
return True
for row in xrange(9):
for col in xrange(9):
if grid[row][col] == 0:
for num in xrange(1, 10):
grid[row][col] = num
if check_sudoku(grid) == True:
if correct_solve(grid) == True:
return True
# there are no numbers which could make
# a valid solution, so backtrack
grid[row][col] = 0
return False
這裏是我試圖解決以不同的方式對問題的另一種功能,但它失敗了,我無法找出是
def buggy_solve(grid, col):
# if there is no more zeros
if found_solution(grid):
return True
# if the col is over 8, make it to 0
if col > 8:
col = 0
for row in xrange(9):
if grid[row][col] == 0:
for num in xrange(1, 10):
grid[row][col] = num
if check_sudoku(grid) == True:
# I tend to move to the next cell, and it seems that
# this is correct.
if buggy_solve(grid, col + 1) == True:
return True
# if there are no valid solutions, backtrack.
grid[row][col] = 0
return False
我試着調試程序和沒」問題找到了有用,順便說一句有沒有什麼好的做法來調試一塊遞歸代碼?
編輯:
這裏是我使用的測試矩陣:
easy = [[2,9,0,0,0,0,0,7,0],
[3,0,6,0,0,8,4,0,0],
[8,0,0,0,4,0,0,0,2],
[0,2,0,0,3,1,0,0,7],
[0,0,0,0,8,0,0,0,0],
[1,0,0,9,5,0,0,6,0],
[7,0,0,0,9,0,0,0,1],
[0,0,1,2,0,0,3,0,6],
[0,3,0,0,0,0,0,5,9]]
我確實調用了'buggy_solve(grid,col + 1)'移動到下一列 – shengy 2012-07-18 10:02:55
@shengy:如果在該列中沒有0的行,則不行 – 2012-07-18 11:02:17
明白了!我嘗試了一個網格,其中有一個填充1到9的列1,問題就出現了。你是如何在這種遞歸回溯代碼中發現這個問題的?調試此類程序的最佳做法是什麼? – shengy 2012-07-19 01:57:05