Created
February 2, 2012 16:57
-
-
Save JRondeau16/1724554 to your computer and use it in GitHub Desktop.
N queens Scheme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
(define (iqueens L a d) ;; gives all of the queen solutions | |
(if (null? L) | |
(list a) | |
(append-map (λ(x) | |
(if(diagonal? x a 1) | |
'() | |
(iqueens (remove x L) (cons x a) 1)) | |
) | |
L))) | |
(define (enumerate-interval l h) ;; produces a list from l to h | |
(if (> l h) | |
'() | |
(cons l (enumerate-interval (+ l 1) h)))) | |
(define (diagonal? col a d) | |
(if (null? a) ;; base case 1 no queens can be attacked | |
#F | |
(if (=(abs(- col (car a)))d) ;; base case 2 queens can be attacked | |
#T | |
(diagonal? col (cdr a) (+ d 1) ;; recursive callto diagonal | |
) | |
) | |
)) | |
(define (q n) | |
(define initialsize (enumerate-interval 1 n)) ;;determines the size of the board to pass into iqueens | |
(define resultsofq (iqueens initialsize '() 1));; calling iqueens | |
resultsofq) | |
(define (test n) | |
(define boardsize (enumerate-interval 0 n)) | |
(define finallist (map q boardsize)) ;; run the function q on all of the various boardsizes | |
(define testresults (map length finallist)) ;;determine length of all the results | |
testresults) | |
(test 12) ;; lists the number of solutions from sizes 0 to 12 | |
;; '(1 1 0 0 2) corresponds to 1 solution for size 0, 1 solution for size 1, etc... | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment