这道题太难了,我自己只完成了 empty-board 这一个定义,其他的函数即使看了别人的答案也研究了半天才搞明白。。
; board-size 指的是正方形棋盘的长
(define (queens board-size)
(define (queen-cols k)
(if (= k 0)
(list empty-board)
(filter
(lambda (positions) (safe? k positions)) ; 每组位置不冲突的皇后的位置构成一个位置的集合
(flatmap
(lambda (rest-of-queens) ; rest-of-queens 表示已经被放置在安全位置的 k-1 个王后的位置的组合
(map (lambda (new-row)
(adjoin-position new-row k rest-of-queens))
(enumerate-interval 1 board-size))) ; 把第 k 列的新位置加进之前的王后的位置组合
(queen-cols (- k 1))))))
(queen-cols board-size))
(define empty-board nil)
; 检查2个位置坐标是否冲突
; 同行比较好判断,只要新王后位置的行跟前 k-1 个王后的行都不同就行,同列同理
; 对角线比较复杂,分为从左上到右下和从左下到右上两类
; 从左上到右下,在同一条对角线上的坐标,行与列坐标之差相同
; 从左下到右上,在同一条对角线上的坐标,行与列坐标之和相同
(define (check pos1 pos2)
(let ((x1 (car pos1))
(y1 (cadr pos1))
(x2 (car pos2))
(y2 (cadr pos2)))
(and (not (= x1 x2))
(not (= y1 y2))
(not (= (- x1 x2) (- y1 y2)))
(not (= (+ x1 x2) (+ y1 y2))))))
; 检查新加入的王后的位置与其他王后的位置是否冲突
; k 其实没有任何作用
(define (safe? k positions)
(let ((new-queen (car positions))
(rest-of-queens (cdr positions)))
(accumulate (lambda (pos result)
(and (check pos new-queen)
result))
true
rest-of-queens)))
; 在已有前 k-1 个王后的位置组合的基础上,把某个位置坐标(row col)加到第一个位置
(define (adjoin-position row col rest-of-queens)
(cons (list row col) rest-of-queens))
(queens 1)
(queens 2)
(queens 3)
(queens 4)
(queens 8)
; 执行结果
'(((1 1)))
'()
'()
'(((1 4) (3 3) (4 2) (2 1)) ((1 4) (4 3) (2 2) (3 1)) ((2 4) (3 3) (1 2) (4 1)) ((3 4) (1 3) (2 2) (4 1)))
'(((1 8) (2 7) (6 6) (3 5) (5 4) (7 3) (8 2) (4 1))
((1 8) (2 7) (4 6) (6 5) (7 4) (3 3) (8 2) (5 1))
((1 8) (2 7) (6 6) (3 5) (7 4) (4 3) (8 2) (5 1))
((1 8) (2 7) (7 6) (3 5) (4 4) (6 3) (8 2) (5 1))
((1 8) (3 7) (4 6) (5 5) (7 4) (2 3) (8 2) (6 1))
((1 8) (4 7) (2 6) (7 5) (5 4) (3 3) (8 2) (6 1))
((1 8) (5 7) (2 6) (4 5) (7 4) (3 3) (8 2) (6 1))
((1 8) (3 7) (7 6) (2 5) (4 4) (5 3) (8 2) (6 1))
((1 8) (2 7) (4 6) (5 5) (8 4) (6 3) (3 2) (7 1))
((1 8) (4 7) (2 6) (5 5) (6 4) (8 3) (3 2) (7 1))
((1 8) (3 7) (6 6) (2 5) (5 4) (8 3) (4 2) (7 1))
((1 8) (2 7) (4 6) (6 5) (8 4) (3 3) (5 2) (7 1))
((1 8) (2 7) (6 6) (3 5) (8 4) (4 3) (5 2) (7 1))
((1 8) (3 7) (6 6) (4 5) (2 4) (8 3) (5 2) (7 1))
((1 8) (6 7) (2 6) (3 5) (4 4) (8 3) (5 2) (7 1))
((1 8) (2 7) (4 6) (8 5) (5 4) (3 3) (6 2) (7 1))
((2 8) (4 7) (1 6) (5 5) (6 4) (7 3) (3 2) (8 1))
((3 8) (1 7) (6 6) (2 5) (5 4) (7 3) (4 2) (8 1))
((3 8) (1 7) (6 6) (4 5) (2 4) (7 3) (5 2) (8 1))
((2 8) (6 7) (1 6) (3 5) (4 4) (7 3) (5 2) (8 1))
((3 8) (1 7) (4 6) (5 5) (7 4) (2 3) (6 2) (8 1))
((2 8) (4 7) (1 6) (7 5) (5 4) (3 3) (6 2) (8 1))
((2 8) (5 7) (1 6) (4 5) (7 4) (3 3) (6 2) (8 1))
((3 8) (1 7) (7 6) (2 5) (4 4) (5 3) (6 2) (8 1))
((4 8) (1 7) (3 6) (5 5) (6 4) (2 3) (7 2) (8 1))
((2 8) (4 7) (5 6) (1 5) (6 4) (3 3) (7 2) (8 1))
((4 8) (1 7) (5 6) (2 5) (6 4) (3 3) (7 2) (8 1))
((5 8) (1 7) (2 6) (4 5) (6 4) (3 3) (7 2) (8 1))
((2 8) (3 7) (6 6) (4 5) (1 4) (5 3) (7 2) (8 1))
((2 8) (4 7) (6 6) (1 5) (3 4) (5 3) (7 2) (8 1))
((4 8) (1 7) (6 6) (2 5) (3 4) (5 3) (7 2) (8 1))
((2 8) (6 7) (3 6) (1 5) (4 4) (5 3) (7 2) (8 1)))
标签:2.42,每日,位置,sicp,rest,王后,board,define,queens
From: https://www.cnblogs.com/think2times/p/18459724