This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: CS 135 Winter 2011 Brad Lushman Assignment: 9 Due: Wed., March 30, 2011 Language level: Intermediate Student with Lambda Allowed recursion: Any Files to submit: nqueens.rkt Warmup exercises: HtDP 28.1.6, 28.2.1, 30.1.1, 31.3.1, 31.3.3, 31.3.6, 32.3.1 Practise exercises: HtDP 28.1.4, 28.2.2, 28.2.3, 28.2.4, 31.3.7, 32.3.2, 32.3.3 Here are the assignment questions you need to submit. Place all solutions (including the bonus, if you attempt it) in the file nqueens.rkt . 1. The main goal of this assignment is to use Scheme to construct a solution of the famous N- queens problem. An instance of the N-queens problem involves an n × n chessboard, and n queens. The goal is to place the n queens on the chessboard such that no queen attacks any other queen. A queen is able to attack any square reachable from it on a vertical, horizontal, or diagonal path. Thus, a solution of the problem involves placing the queens such that no two lie on the same row, column, or diagonal on the board. For more details, see http:// en.wikipedia.org/wiki/N_queens and http://en.wikipedia.org/wiki/ Queen_(chess) . We can formulate the N-queens problem in terms of graph search. Conceptually, the ver- tices (nodes) of the graph consist of board configurations (possible placements of queens on the boards), and there is an edge from configuration C 1 to configuration C 2 if C 2 can be obtained from C 1 by adding a queen to an unattacked square. The initial vertex of the graph corresponds to the empty chessboard, and a final vertex is any vertex in which there are no unattacked squares. A solution of the N-queens problem is a path from the initial vertex to a final vertex, of length n . To construct a solution for this problem in Scheme, we first consider how to represent a board configuration. A convenient approach, and the one we will use, is the following: ;; A board is a ( listof posn ). We represent a board by a list of squares (denoted by posn s) on the board not attacked by a queen. The square at row i , column j (where i and j range from 0 to n- 1 ) is denoted by the posn ( make-posn i j ). Thus, for example, • the initial board is a list containing one posn for every square on the board; • a 3 × 3 board with a queen at (0 , 0) is represented by the list (possibly reordered) ( list ( make-posn 1 2 ) ( make-posn 2 1 )) • a final board (one in which every square is attacked by a queen) is represented by...
View Full Document
This note was uploaded on 10/02/2011 for the course CS 135 taught by Professor Vasiga during the Winter '07 term at Waterloo.
- Winter '07