Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #lang racket
- (define empty-queens-pattern '(()))
- (define (pos r c) (list r c))
- (define (row-of p) (car p))
- (define (col-of p) (cadr p))
- (define (vertical=? p1 p2)
- (= (col-of p1) (col-of p2)))
- (define (horizontal=? p1 p2)
- (= (row-of p1) (row-of p2)))
- (define (diagonal=? p1 p2)
- (or (= (+ (row-of p1) (col-of p1))
- (+ (row-of p2) (col-of p2)))
- (= (- (row-of p1) (col-of p1))
- (- (row-of p2) (col-of p2)))))
- (define (attackable? p1 p2)
- (or (vertical=? p1 p2)
- (horizontal=? p1 p2)
- (diagonal=? p1 p2)))
- (define (safe? p1 ps)
- (andmap (λ (p2) (not (attackable? p1 p2))) ps))
- (define (n-queen n)
- (for/fold ([pattern empty-queens-pattern])
- ([r (in-range n)])
- (for*/list ([queens pattern]
- [c (in-range n)]
- #:when (safe? (pos r c) queens))
- (cons (pos r c) queens))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement