SICP Exercise 2.42: Eight queens puzzle
2021-10-29
Categories: Programming
Exercise 2.42: The “eight-queens puzzle” asks how to place eight queens on a chessboard so that no queen is in check from any other (i.e., no two queens are in the same row, column, or diagonal).
One way to solve the puzzle is to work across the board, placing a queen in each column. Once we have placed
k - 1queens, we must place thekth queen in a position where it does not check any of the queens already on the board. We can formulate this approach recursively: Assume that we have already generated the sequence of all possible ways to placek - 1queens in the firstk - 1columns of the board. For each of these ways, generate an extended set of positions by placing a queen in each row of thekth column. Now filter these, keeping only the positions for which the queen in thekth column is safe with respect to the other queens. This produces the sequence of all ways to placekqueens in the firstkcolumns. By continuing this process, we will produce not only one solution, but all solutions to the puzzle.We implement this solution as a procedure
queens, which returns a sequence of all solutions to the problem of placingnqueens on ann×nchessboard. Queens has an internal procedurequeen-colsthat returns the sequence of all ways to place queens in the firstkcolumns of the board.(define (queen board-size) (define (queen-cols k) (if (= k 0) (list empty-board) (filter (lambda (positions) (safe? k positions)) (flatmap (lambda (rest-of-queens) (map (lambda (new-row) (adjoin-position new-row k rest-of-queens)) (enumerate-interval 1 board-size))) (queen-cols (- k 1)))))) (queen-cols board-size))In this procedure
rest-of-queensis a way to placek - 1queens in the firstk - 1columns, andnew-rowis a proposed row in which to place the queen for thekth column. Complete the program by implementing the representation for sets of board positions, including the procedureadjoin-position, which adjoins a new row-column position to a set of positions, andempty-board, which represents an empty set of positions. You must also write the proceduresafe?, which determines for a set of positions, whether the queen in thekth column is safe with respect to the others. (Note that we need only check whether the new queen is safe – the other queens are already guaranteed safe with respect to each other.)
I will try to implement the simpler procedures first.
empty-board represents an empty set of positions:
(define empty-board '())
adjoin-position adjoins a new row-column position to a set of positions:
(define (adjoin-position row col rest-of-queens)
(cons (list row col) rest-of-queens))
attack? checks if two queens are in the same row, column, or diagonal:
(define (attack? q1 q2)
(or (= (row q1) (row q2))
(= (abs (- (row q1) (row q2)))
(abs (- (col q1) (col q2))))))
(define (row position)
(car position))
(define (col position)
(cadr position))
(No need to check if they are in the same column)
The most difficult procedure is safe?: which determines for a set of positions, whether the queen in the kth column is safe with respect to the others.
The steps should be something like this:
- get the queen in the
kth column - get the first element of the
rest-of-queens - check if two queens are under attack
- if yes, return
#f(false) - else: remove the first element from
rest-of-queensand do the same with the remaining elements
- if yes, return
Here’s the code:
(define (safe? k positions)
(if (= (length positions) 1)
#t
(if (attack? (k-queen positions) (first-queen positions))
#f
(safe? k (remove (first-queen positions) positions)))))
(define (k-queen positions)
(car positions))
(define (first-queen positions)
(cadr positions))
The remove procedure is similar to the one at the end of Nested Mappings section but uses equals? to compare list:
(trace-define (remove item sequence)
(filter (lambda (x) (not (equal? x item)))
sequence))
The completed program:
(trace-define (queen board-size)
(define (queen-cols k)
(if (= k 0)
(list empty-board)
(filter
(lambda (positions) (safe? k positions))
(flatmap
(lambda (rest-of-queens)
(map (lambda (new-row)
(adjoin-position new-row k rest-of-queens))
(enumerate-interval 1 board-size)))
(queen-cols (- k 1))))))
(trace queen-cols)
(queen-cols board-size))
(define empty-board '())
(define (adjoin-position row col rest-of-queens)
(cons (list row col) rest-of-queens))
(define (safe? k positions)
(if (= (length positions) 1)
#t
(if (attack? (k-queen positions) (first-queen positions))
#f
(safe? k (remove (first-queen positions) positions)))))
(define (k-queen positions)
(car positions))
(define (first-queen positions)
(cadr positions))
(define (attack? q1 q2)
(or (= (row q1) (row q2))
(= (abs (- (row q1) (row q2)))
(abs (- (col q1) (col q2))))))
(define (row position)
(car position))
(define (col position)
(cadr position))
(define (remove item sequence)
(filter (lambda (x) (not (equal? x item)))
sequence))
Test:
$ racket 2.42-eight-queens.rkt
>(queen 4)
>(queen-cols 4)
> (queen-cols 3)
> >(queen-cols 2)
> > (queen-cols 1)
> > >(queen-cols 0)
< < <'(())
< < '(((1 1)) ((2 1)) ((3 1)) ((4 1)))
< <'(((3 2) (1 1))
((4 2) (1 1))
((4 2) (2 1))
((1 2) (3 1))
((1 2) (4 1))
((2 2) (4 1)))
< '(((2 3) (4 2) (1 1))
((1 3) (4 2) (2 1))
((4 3) (1 2) (3 1))
((3 3) (1 2) (4 1)))
<'(((3 4) (1 3) (4 2) (2 1)) ((2 4) (4 3) (1 2) (3 1)))
'(((3 4) (1 3) (4 2) (2 1)) ((2 4) (4 3) (1 2) (3 1)))
|---|---|---|---|
| | | X | |
|---|---|---|---|
| X | | | |
|---|---|---|---|
| | | | X |
|---|---|---|---|
| | X | | |
|---|---|---|---|
|---|---|---|---|
| | X | | |
|---|---|---|---|
| | | | X |
|---|---|---|---|
| X | | | |
|---|---|---|---|
| | | X | |
|---|---|---|---|
Related Posts:
- SICP Exercise 2.77: expected a procedure that can be applied to arguments, given #f
- SICP Exercise 2.43: Eight queens: interchange the order of the nested mappings
- SICP Exercise 2.41: Triple sum
- SICP Exercise 2.35: Counting leaves of a tree
- SICP Exercise 2.27: Reversing nested lists
Quan Tong