SHARE
TWEET

Untitled

a guest Jul 22nd, 2019 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #lang racket
  2.  
  3. (define empty-queens-pattern '(()))
  4.  
  5. (define (pos r c) (list r c))
  6.  
  7. (define (row-of p) (car p))
  8.  
  9. (define (col-of p) (cadr p))
  10.  
  11. (define (vertical=? p1 p2)
  12.   (= (col-of p1) (col-of p2)))
  13.  
  14. (define (horizontal=? p1 p2)
  15.   (= (row-of p1) (row-of p2)))
  16.  
  17. (define (diagonal=? p1 p2)
  18.   (or (= (+ (row-of p1) (col-of p1))
  19.          (+ (row-of p2) (col-of p2)))
  20.       (= (- (row-of p1) (col-of p1))
  21.          (- (row-of p2) (col-of p2)))))
  22.  
  23. (define (attackable? p1 p2)
  24.   (or (vertical=? p1 p2)
  25.       (horizontal=? p1 p2)
  26.       (diagonal=? p1 p2)))
  27.  
  28. (define (safe? p1 ps)
  29.   (andmap (λ (p2) (not (attackable? p1 p2))) ps))
  30.  
  31. (define (n-queen n)
  32.   (for/fold ([pattern empty-queens-pattern])
  33.             ([r (in-range n)])
  34.     (for*/list ([queens pattern]
  35.                 [c (in-range n)]
  36.                 #:when (safe? (pos r c) queens))
  37.       (cons (pos r c) queens))))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top