Information Technology Reference
In-Depth Information
Problem 2. Eight Queens. Place 8 queens on the chess board so that they do not
attack each other.
We present here a solution that uses constraints. We only write the part
of the program that generates constraints. The code that actually solves the
generated constraints would make use of the built-in INDOMAIN as explained in
Subsection 6.2.
CONST N = 8;
TYPE Board = ARRAY [1..N] OF CONSTRAINED [1..N];
VAR i, j: [1..N];
X: Board;
BEGIN
FOR i := 1 TO N-1 DO
FOR j := i+1 TO N DO
X[i] <> X[j];
X[i] <> X[j]+j-i;
X[i] <> X[j]+i-j
END
END
END;
Each generated constraint is thus of the form X[i] <> X[j] or X[i] <>
X[j] + k for some values i,j 2 [1..N] such that i < j and k being either the
value of j-i or of i-j .
Note that the above program text coincides with the problem formulation.
Next, consider the following problem that deals with the equations arising
when studying the flow of heat.
Problem 3. Laplace Equations . Given is a two dimensional grid with given values
for all the exterior points. The value of each interior points equals the average
of the values of its four neighbours. Compute the value of all interior points.
The solution using constraints again truly coincides with the problem spec-
ication. It is conceptually much simpler than the solution based on constraint
logic programming and given in [10].
TYPE Board = ARRAY [1..M], [1..N] OF CONSTRAINED REAL;
VAR i:[1..M];
j:[1..N];
X: Board;
BEGIN
FOR i := 2 TO M-1 DO
FOR j := 2 TO N-1 DO
X[i,j] = (X[i+1,j] + X[i-1,j] + X[i,j+1] + X[i,j-1])/4
END
END
END;
 
Search WWH ::




Custom Search