• No results found

The eight queens puzzle is the problem of placing eight chess queens on a chessboard so that no two queens threaten each other. In other words, no two queens can share the same row, column, or diagonal. Solutions to this puzzle are represented by the stable models of the program in Listing 3.20.

In the puzzle known as Hidato, or Number Snake, you are given a grid partially filled with integers, for instance:

3.10. SEARCH IN TWO DIMENSIONS 53

Listing 3.19: Hamiltonian cycles in a directed graph 1 % H a m i l t o n i a n c y c l e s in a d i r e c t e d g r a p h .

2

3 % i n p u t : set v e r t e x /1 of v e r t i c e s of a g r a p h G ;

4 % set e d g e /2 of e d g e s of G ; a v e r t e x v0 of G .

5

6 % in ( X , Y ) is the set of e d g e s i n c l u d e d in the c y c l e .

7

8 { in ( X , Y ) : e d g e ( X , Y )} = 1 : - v e r t e x ( X ).

9 { in ( X , Y ) : e d g e ( X , Y )} = 1 : - v e r t e x ( Y ).

10 % a c h i e v e d : a set in /2 of e d g e s is s e l e c t e d s u c h t h a t e v e r y

11 % v e r t e x of G is the h e a d of e x a c t l y one e d g e f r o m

12 % in /2 , and the t a i l of e x a c t l y one e d g e f r o m in /2.

13 14 r e a c h a b l e ( X ) : - in ( v0 , X ). 15 r e a c h a b l e ( Y ) : - r e a c h a b l e ( X ) , in ( X , Y ). 16 % a c h i e v e d : r e a c h a b l e ( X ) iff t h e r e e x i s t s a p a t h of non - z e r o 17 % l e n g t h f r o m v0 to X s u c h t h a t all its e d g e s 18 % b e l o n g to in /2. 19 20 : - not r e a c h a b l e ( X ) , v e r t e x ( X ). 21 % a c h i e v e d : all v e r t i c e s of G b e l o n g to r e a c h a b l e /1. 22 23 # s h o w in /2.

Listing 3.20: Eight queens 1 % E i g h t q u e e n s p u z z l e . 2 3 { q ( 1 . . 8 , 1 . . 8 ) } = 8. 4 % a c h i e v e d : q /2 is a set of 8 s q u a r e s on the c h e s s b o a r d . 5 6 : - q ( R , C1 ) , q ( R , C2 ) , C1 < C2 . 7 : - q ( R1 , C ) , q ( R2 , C ) , R1 < R2 . 8 % a c h i e v e d : q /2 c o n t a i n s at m o s t one s q u a r e in e a c h c o l u m n

9 and at m o s t one in e a c h row .

10

11 : - q ( R1 , C1 ) , q ( R2 , C2 ) , R1 < R2 , | R1 - R2 | = | C1 - C2 |.

6

2 8

1

Table 3.1: A Number Snake puzzle

The goal is to fill the grid so that consecutive numbers connect horizontally, vertically, or diagonally.

Exercise 3.16. Solve the puzzle in Table 3.1.

The program shown in Listing 3.21 solves the Number Snake puzzles in which the grid is a square of sizen, and the numbers go from 1 ton2. The inequality in Line 14 expresses that the distance between the centers of the squares (R1,C1) and (R2,C2) is less than or equal to√2; this is equivalent to saying that the squares connect horizontally, vertically, or diagonally.

Exercise 3.17. Cover a chessboard by twenty-one 3×1 tiles and one 1×1 tile.

The program in Listing 3.22 solves the problem from Exercise 3.17. This encoding describes all possible ways to place twenty-one 3×1 tiles on the board without overlaps; it does not mention the 1×1 tile explicitly. The expressionsh(1..6,1..8)andv(1..8,1..6)

represent the 48 possible positions of horizontal tiles and the 48 possible positions of vertical tiles, so that the choice rule in Line 7 requires the total number of tiles, both horizontal and vertical, to be 21.

Addition in Line 10 is applied to a number and an interval—to C and 1..2. In the language ofclingo, this is understood as forming the set of all integers that can be obtained by addingC and a number from the interval1..2. In other words, this expression has the same meaning as C+1..C+2. Similarly, R-(0..2) in Line 16 has the same meaning as

R-2..R. Applying arithmetic operations to intervals in the language ofclingois discussed in Section 4.6 in more detail.