码迷,mamicode.com
首页 > 其他好文 > 详细

Scheme N皇后

时间:2014-08-12 16:24:34      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   div   log   ad   ef   

(define (range n)
    (define (recur n)
        (if (= n -1)
            ()
            (cons n (recur (- n 1)))))
    (recur (- n 1)))

(define (flatten a)
    (if (null? a)
        ()
        (append (car a) (flatten (cdr a)))))

(define (safe? x y sln)
    (if (null? sln)
        #t
        (let ((px (car (car sln))) (py (cadr (car sln))))
            (if (or (= y py) (= (- py y) (- px x)) (= (- py y) (- x px)))
                #f 
                (safe? x y (cdr sln))))))

(define (nqueen n)
    (define (recur x)
        (if (= x -1)
            (list ())
            (flatten (map (lambda (y) (map (lambda (sln) (cons (list x y) sln)) (filter (lambda (sln) (safe? x y sln)) (recur (- x 1))))) (range n)))))
    (recur (- n 1)))

(define (pl a)
    (if (null? a)
        ()
        (begin (display (car a)) (display "\n") (pl (cdr a)))))

(pl (nqueen 4))

 

Scheme N皇后,布布扣,bubuko.com

Scheme N皇后

标签:style   blog   color   ar   div   log   ad   ef   

原文地址:http://www.cnblogs.com/xiangnan/p/3907175.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!