A famous problem in the game of chess concerns the placements of queens on a
board. For our purposes, a chess board is a ``square'' of, say,
eight-by-eight or three-by-three tiles. The queen is a game piece that can
move in a horizontal, vertical, or diagonal direction arbitrarily far. We say
that a queen <#37264#>threatens <#37264#> a tile if it is on the tile or can move to
it. Figure~#figqueens#37265> shows an example. The solid disk represents a
queen in the second column and sixth row. The solid lines radiating from the
disk go through all those tiles that are threatened by the queen.
#picture37267#
<#37283#>Figure: A chess board with a single queen<#37283#>
The queen-placement problem is to place eight queens on a chess board of
eight-by-eight tiles such that the queens on the board don't threaten each
other. In computing, we generalize the problem of course and ask whether we
can place <#66922#><#37285#>n<#37285#><#66922#> queens on some board of arbitrary size <#66923#><#37286#>m<#37286#><#66923#> by <#66924#><#37287#>m<#37287#><#66924#>.
Even a cursory glance at the problem suggests that we need a data
representation of boards and some basic functions on boards before we can
even think of designing a program that solves the problem. Let's start with
some basic data and function definitions.
<#37290#>Exercise 28.2.1<#37290#>
Develop a data definition for chess boards.
<#37292#>Hint:<#37292#> Use lists. Represent tiles with <#66925#><#37293#>true<#37293#><#66925#> and <#66926#><#37294#>false<#37294#><#66926#>. A value
of <#66927#><#37295#>true<#37295#><#66927#> should indicate that a position is available for the
placement of a queen; <#66928#><#37296#>false<#37296#><#66928#> should indicate that a position is occupied
by, or threatened by, a queen.~ Solution<#66929#><#66929#>
Next we need a function for creating a board and another one for checking on
a specific tile. Following the examples of lists, let's define
<#66930#><#37302#>build-board<#37302#><#66930#> and <#66931#><#37303#>board-ref<#37303#><#66931#>.
<#37304#>Exercise 28.2.2<#37304#>
Develop the following two functions on chess boards:
<#71536#>;; <#66932#><#37310#>build-board<#37310#> <#37311#>:<#37311#> <#37312#>N<#37312#> <#37313#>(<#37313#><#37314#>N<#37314#> <#37315#>N<#37315#> <#37316#><#37316#><#37317#>-;SPMgt;<#37317#><#37318#><#37318#> <#37319#>boolean)<#37319#> <#37320#><#37320#><#37321#>-;SPMgt;<#37321#><#37322#><#37322#> <#37323#>board<#37323#><#66932#><#71536#>
<#71537#>;; to create a board of size <#66933#><#37324#>n<#37324#><#66933#> x <#66934#><#37325#>n<#37325#><#66934#>, <#71537#>
<#71538#>;; fill each position with indices <#66935#><#37326#>i<#37326#><#66935#> and <#66936#><#37327#>j<#37327#><#66936#> with <#66937#><#37328#>(f<#37328#> <#37329#>i<#37329#> <#37330#>j)<#37330#><#66937#><#71538#>
<#37331#>(define<#37331#> <#37332#>(build-board<#37332#> <#37333#>n<#37333#> <#37334#>f)<#37334#> <#37335#>...)<#37335#>
<#71539#>;; <#66938#><#37336#>board-ref<#37336#> <#37337#>:<#37337#> <#37338#>board<#37338#> <#37339#>N<#37339#> <#37340#>N<#37340#> <#37341#><#37341#><#37342#>-;SPMgt;<#37342#><#37343#><#37343#> <#37344#>boolean<#37344#><#66938#><#71539#>
<#71540#>;; to access a position with indices <#66939#><#37345#>i<#37345#><#66939#>, <#66940#><#37346#>j<#37346#><#66940#> on a-board<#71540#>
<#37347#>(define<#37347#> <#37348#>(board-ref<#37348#> <#37349#>a-board<#37349#> <#37350#>i<#37350#> <#37351#>j)<#37351#> <#37352#>...)<#37352#>
Test them rigorously! Use the strategy of section~#secequaltest#37356> to
formulate the tests as boolean-valued expressions.~ Solution<#66941#><#66941#>
In addition to these generic functions on a chess board representation, we
also need at least one function that captures the concept of a ``threat'' as
mentioned in the problem statement.
<#37362#>Exercise 28.2.3<#37362#>
Develop the function <#66942#><#37364#>threatened?<#37364#><#66942#>, which computes whether a queen
can reach a position on the board from some given position. That is, the
function consumes two positions, given as <#66943#><#37365#>posn<#37365#><#66943#> structures, and
produces <#66944#><#37366#>true<#37366#><#66944#> if a queen on the first position can threaten the
second position.
<#37367#>Hint:<#37367#> \ The exercise translate the chess problem of ``threatening queens''
into the mathematical problem of determining whether in some given grid,
two positions are on the same vertical, horizontal, or diagonal line. Keep
in mind that each position belongs to two diagonals and that the slope of a
diagonal is either <#66945#><#37368#>+1<#37368#><#66945#> or <#66946#><#37369#>-1<#37369#><#66946#>.~ Solution<#66947#><#66947#>
Once we have data definitions and functions for the ``language of chess
boards,'' we can turn our attention to the main task: the algorithm for
placing a number of queens on some given board.
<#37375#>Exercise 28.2.4<#37375#>
Develop <#66948#><#37377#>placement<#37377#><#66948#>. The function consumes a natural number and a
board and tries to place that many queens on the board. If the queens can
be placed, the function produces an appropriate board. If not, it produces
<#66949#><#37378#>false<#37378#><#66949#>.~ Solution<#66950#><#66950#>