Note that there are some explanatory texts on larger screens.

plurals
  1. POScheme, N-queens optimization stragegies SICP chapter 2
    primarykey
    data
    text
    <p>SICP contains an partially complete example of the n-queens solutions, by walking a tree of every possible queen placement in the last row, generating more possible positions in the next row to combine the results so far, filtering the possibilities to keep only ones where the newest queen is safe, and repeating recursively. </p> <p>This strategy blows up after about n=11 with a maximum recursion error.</p> <p>I've implemented an alternate strategy that does a smarter tree-walk from the first column, generating possible positions from a list of unused rows, consing each position-list onto an updated list of yet-unused rows. Filtering those pairs considered safe, and recursively mapping over these pairs for the next column. This doesn't blow up (so far) but n=12 takes a minute and n=13 takes about 10 minutes to solve.</p> <pre><code>(define (queens board-size) (let loop ((k 1) (pp-pair (cons '() (enumerate-interval 1 board-size)))) (let ((position (car pp-pair)) (potential-rows (cdr pp-pair))) (if (&gt; k board-size) (list position) (flatmap (lambda (pp-pair) (loop (++ k) pp-pair)) (filter (lambda (pp-pair) (safe? k (car pp-pair))) ;keep only safe (map (lambda (new-row) (cons (adjoin-position new-row k position) (remove-row new-row potential-rows))) ;make pp-pair potential-rows))))))) ;auxiliary functions not listed </code></pre> <p>Not really looking for code, but a simple explanation of a strategy or two that's less naive and that clicks well with a functional approach. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload