Skip to content

Commit

Permalink
4.44
Browse files Browse the repository at this point in the history
  • Loading branch information
jiacai2050 committed Mar 29, 2016
1 parent 408f1ce commit 3e32578
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions exercises/04/4.44.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
; 本题和 2.42 表示方法一样,用一个list表示一种解法,比如四皇后问题,解法(A B C D)表示,第一个皇后在位置A,第二个皇后在位置B,第三个皇后在位置C,第四个皇后在位置D,用list的位置表示了皇后的次序。

; https://github.com/jiacai2050/sicp/blob/master/exercises/02/2.42_2.43.md#242


(define (check-current-diagonal? current-postion rest distance-to-current)
(if (null? rest)
#f
(or (= (- current-postion distance-to-current) (car rest))
(= (+ current-postion distance-to-current) (car rest))
(check-current-diagonal? current-postion (cdr rest) (+ distance-to-current 1)))))

(define (in-diagonal? positions)
(if (null? positions)
#f
(or (in-diagonal (car positions) (cdr positions) 1)
(in-diagonal? (cdr positions)))))

(define (collide? positions)
(or (not (distinct? positions))
(in-diagonal? positions)))

(define (eight-queen)
(let ((q1 (amb 1 2 3 4 5 6 7 8))
(q2 (amb 1 2 3 4 5 6 7 8))
(q3 (amb 1 2 3 4 5 6 7 8))
(q4 (amb 1 2 3 4 5 6 7 8))
(q5 (amb 1 2 3 4 5 6 7 8))
(q6 (amb 1 2 3 4 5 6 7 8))
(q7 (amb 1 2 3 4 5 6 7 8))
(q8 (amb 1 2 3 4 5 6 7 8)))
(require (not (collide? (list q1 q2 q3 q4 q5 q6 q7 q8))))))

0 comments on commit 3e32578

Please sign in to comment.