注:这是一个唐氏实现。要看更高效的,请直接拉到最底下。
(define (solve-n-queens board-size)
(define solutions '()) ; 用于存储所有解的列表
; queens-placement 是一个向量,索引代表行 (0 到 board-size-1),
; 值代表该行皇后所在的列 (0 到 board-size-1)
(define queens-placement (make-vector board-size 0))
; 检查在 (row, col) 放置皇后是否安全
; 只需检查前面已经放置的皇后 (0 到 row-1行)
(define (is-safe? current-row current-col)
(let loop ((prev-row 0))
(if (>= prev-row current-row) ; 如果检查完了所有之前的行,则是安全的
#t
(let ((prev-col (vector-ref queens-placement prev-row)))
(if (or (= prev-col current-col) ; 同一列
(= (abs (- current-row prev-row)) (abs (- current-col prev-col)))) ; 同一对角线
#f ; 发现冲突
(loop (+ prev-row 1))))))) ; 继续检查下一行
; 递归辅助函数,尝试在 current-row 放置皇后
(define (solve-n-queens-helper current-row)
(if (= current-row board-size)
; 所有皇后都已放置,找到了一个解
(let ((solution-list
(map (lambda (col-0-indexed) (+ col-0-indexed 1)) ; 转换为1-indexed
(vector->list queens-placement))))
(set! solutions (cons solution-list solutions))) ; 将解(列表形式)加入solutions
; 否则,尝试在 current-row 的每一列放置皇后
(let loop ((col 0)) ; col 从 0 到 board-size-1
(when (< col board-size)
(if (is-safe? current-row col)
(begin
(vector-set! queens-placement current-row col) ; 放置皇后
(solve-n-queens-helper (+ current-row 1)) ; 递归到下一行
; 回溯:此处的 queens-placement[current-row] 会在下一次 col 循环中被覆盖,
; 或者当这个递归调用返回时,上一层调用会继续它的 col 循环。
))
(loop (+ col 1)))))) ; 尝试下一列
(solve-n-queens-helper 0) ; 从第0行开始
(reverse solutions)) ; solutions 是逆序构建的,所以最后反转一下得到正序
; --- 主程序和输出 ---
(define (main)
(let* ((board-size 10) ; 您可以改变这个值,例如 4, 5, ...
;; 打印计算提示信息 (这是一个副作用操作)
(_ (begin
(display (string-append "正在计算 " (number->string board-size) " 皇后的解...\n"))
(flush-output-port (current-output-port)))) ; 确保信息立即显示
(all-solutions (solve-n-queens board-size)))
(if (null? all-solutions)
(display "未找到解。\n")
(begin
(display (string-append "共找到 " (number->string (length all-solutions))
" 个解。对于 " (number->string board-size) "x"
(number->string board-size) " 的棋盘:\n"))
;; 使用命名 let 来迭代打印解和计数
(let print-solutions ((solutions-list all-solutions) (solution-number 1))
(unless (null? solutions-list)
(let ((current-solution (car solutions-list)))
(display (string-append "\n解 #" (number->string solution-number) ":\n"))
;; 输出一个解:共 board-size 行,第 i 行是皇后在第 i 行的列号 (1-indexed)
(for-each
(lambda (queen-col)
(display queen-col)
(newline))
current-solution)
(print-solutions (cdr solutions-list) (+ solution-number 1))))))))
(newline) ; 在所有输出结束后换行,使终端提示符在新的一行
)
; 运行主程序
(main)
更高效的实现:
(define (solve-n-queens board-size)
(define solutions '())
(define queens-placement (make-vector board-size 0)) ; 存储每行皇后的列位置
; 用于O(1)检查
(define col-used (make-vector board-size #f)) ; 列是否被占用
(define diag1-used (make-vector (- (* 2 board-size) 1) #f)) ; 主对角线 (row + col)
(define diag2-used (make-vector (- (* 2 board-size) 1) #f)) ; 副对角线 (row - col + board-size - 1)
(define (solve-n-queens-helper current-row)
(if (= current-row board-size)
(let ((solution-list
(map (lambda (col-0-indexed) (+ col-0-indexed 1))
(vector->list queens-placement))))
(set! solutions (cons solution-list solutions)))
(let loop ((col 0)) ; col 从 0 到 board-size-1
(when (< col board-size)
(let ((diag1-idx (+ current-row col))
(diag2-idx (+ (- current-row col) (- board-size 1))))
(when (and (not (vector-ref col-used col))
(not (vector-ref diag1-used diag1-idx))
(not (vector-ref diag2-used diag2-idx)))
(vector-set! queens-placement current-row col)
(vector-set! col-used col #t)
(vector-set! diag1-used diag1-idx #t)
(vector-set! diag2-used diag2-idx #t)
(solve-n-queens-helper (+ current-row 1))
; 回溯
(vector-set! col-used col #f)
(vector-set! diag1-used diag1-idx #f)
(vector-set! diag2-used diag2-idx #f)
; queens-placement[current-row] 不需要显式清除,因为它会在下一次 col 循环中被覆盖
))
(loop (+ col 1))))))
(solve-n-queens-helper 0)
(reverse solutions))
; --- 主程序和输出 (与你原版类似,略作调整以匹配新的 solve-n-queens) ---
(define (main)
(let* ((board-size 13) ; 例如 13
(_ (begin
(display (string-append "正在计算 " (number->string board-size) " 皇后的解...\n"))
(flush-output-port (current-output-port))))
(all-solutions (solve-n-queens board-size)))
(display (string-append "共找到 " (number->string (length all-solutions)) " 个解。\n"))
; (如果需要打印前几个解)
(let print-first-n ((solutions-list all-solutions) (n 3) (count 1))
(when (and (not (null? solutions-list)) (> n 0))
(display (string-append "\n解 #" (number->string count) ": "))
(display (car solutions-list)) (newline)
(print-first-n (cdr solutions-list) (- n 1) (+ count 1))))
(newline)))
(time (main))
(exit)