Recursion 方案中的数独回溯算法

Recursion 方案中的数独回溯算法,recursion,scheme,racket,sudoku,backtracking,Recursion,Scheme,Racket,Sudoku,Backtracking,我的数独解算器函数有一个问题,它没有执行回溯过程,我不明白为什么,当回溯到来时,函数停止,下面是代码: (define (solve-sudoku grid) (define blank-space (check-for-empty-space 0 '())) (cond [(empty? blank-space) (begin (display "FIN\n") true)]) (define x (first blank-space)) (define y (first (rest

我的数独解算器函数有一个问题,它没有执行回溯过程,我不明白为什么,当回溯到来时,函数停止,下面是代码:

(define (solve-sudoku grid)
 (define blank-space (check-for-empty-space 0 '()))
 (cond [(empty? blank-space) (begin (display "FIN\n") true)])
 (define x (first blank-space))
 (define y (first (rest blank-space)))
 (display x) (display y) (display "\n")
 (cond [(eqv? #f (try-value grid 1 x y )) false])
)

(define (try-value grid num  x y )
  (cond [(> num 9) false]
       [(is-safe grid num x y) 
        (begin 
          (assign-to-pos grid num x y) 
           (cond [(solve-sudoku grid) true]
                 [else (begin (display "reset\n")  
                       (assign-to-pos grid 0 x y))]
               ))]
    [else (try-value grid (+ 1 num) x y)])
  )
我有一个测试矩阵:

-----------+------+------+------+----
(定义行0(向量3 0 6 5 0 8 4 0 0))
(定义行1(向量5 2 0 0 0 0))
(定义第2行(向量0 8 7 0 0 0 3 1))
;---------------+-----+-----+-----+--
(定义行3(向量0 0 3 0 1 0 0 8 0))
(定义第4行(向量9 0 8 6 3 0 5))
(定义第5行(向量05090600))
;---------------+-----+-----+-----+--
(定义第6行(向量1 3 0 0 0 0 2 5 0))
(定义第7行(向量0 0 0 0 7 4))
(定义第8行(向量0 0 5 2 0 6 3 0 0))
;---------------+-----+-----+-----+--
(定义栅格(矢量行0第1行第2行第3行第4行第5行第6行第7行第8行))

输出为:

空位置:0,1
空位置:0,4
空位置:0,7
空位置:0,8
空位置:1,2
空位置:1,3
空位置:1,4
空位置:1,5
空位置:1,6
空位置:1,7
空位置:1,8
重置

结果
“#”(#(3 1 6 5 2 8 4 9 7)
#(52413700)
#(08700031)
#(0 0 3 0 1 0 8 0)
#(9 0 8 6 3 0 5)
#(05090600)
#(13000250)
#(0 0 0 7 4)
#(0 0 5 2 0 6 3 0 0))

您的测试用例非常庞大,我可以理解为什么很难找出哪里出了问题。与其尝试调试整个庞大的东西,不如先测试小部件。我看不到您的所有程序,但听起来您需要为程序中的所有函数编写许多小测试