1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % ECLiPSe library for solving "crossfigures" puzzles. % % "Crossfigures" puzzles correspond to problem 21 in the CSPLib. % See www.csplib.org for more details. % % Particular instances can be found at thinks.com/crosswords/xfig.htm. % % This module written by Warwick Harvey, IC-Parc, wh@icparc.ic.ac.uk. % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% :- module(crossfig). :- export across/6, down/6, init_matrix/3, print_matrix/1. :- export square/1, prime/1. :- lib(fd). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % The problem is modelled using an array `Matrix' to represent the puzzle % "board". A second array `Template' is used to indicate whether each % element of `Matrix' should contain a digit or should be blank. This % information can also be used to perform some integrity checks, to help % catch errors in the expression of a problem. % % The multidigit numbers used in the "clues" (1 across, 7 down, etc.) are % set up using the predicates `across/6' and `down/6', which relate these % numbers to the digits in `Matrix'. Once these are all set up, % `init_matrix/3' should be called to complete the initialisation of % `Matrix', before the clue constraints are added. % % Also provided are a number of predicates which are useful for % expressing clue constraints such as "A square number" and "A prime % number". % % See one of the accompanying problem modules (cf*.pl) for an example of % how it all works. % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % across(Matrix, Template, Across, Len, Row, Col): % Constrains `Across' to be equal to the number represented by the % `Len' digits starting at position (Row, Col) of the array `Matrix' % and proceeding across. % `Template' is used for integrity checking, as well as for collecting % information about which elements of `Matrix' should contain digits, % and which should be empty. % across( Matrix , Template , Across , Len , Row , Col ) :- % Constrain `Across' to be equal to the corresponding digits. ( for ( I , Len -1, 0, -1), fromto (1, Mult , NewMult , _ ), fromto (0, SumIn , SumOut , Across ), param ( Matrix , Row , Col ) do Elem is Matrix [ Row , Col + I ], Elem :: [0..9], SumOut #= SumIn + Mult * Elem , NewMult is Mult * 10 ), % Integrity checks. dim( Template , [ _Height , Width ]), ( Template [ Row , Col .. Col + Len - 1] :: 1, ( Col > 1 -> Template [ Row , Col - 1] :: 0 ; true ), ( Col + Len =< Width -> Template [ Row , Col + Len ] :: 0 ; true ) -> true ; printf(error, "Crossfigure integrity violation adding " "an across figure of length %d,%n" "starting at (%d, %d)%n" , [ Len , Row , Col ]), abort ). % % down(Matrix, Template, Down, Len, Row, Col): % Constrains `Down' to be equal to the number represented by the % `Len' digits starting at position (Row, Col) of the array `Matrix' % and proceeding down. % `Template' is used for integrity checking, as well as for collecting % information about which elements of `Matrix' should contain digits, % and which should be empty. % down( Matrix , Template , Down , Len , Row , Col ) :- % Constrain `Down' to be equal to the corresponding digits. ( for ( I , Len -1, 0, -1), fromto (1, Mult , NewMult , _ ), fromto (0, SumIn , SumOut , Down ), param ( Matrix , Row , Col ) do Elem is Matrix [ Row + I , Col ], Elem :: [0..9], SumOut #= SumIn + Mult * Elem , NewMult is Mult * 10 ), % Integrity checks. dim( Template , [ Height , _Width ]), ( Template [ Row .. Row + Len - 1, Col ] :: 1, ( Row > 1 -> Template [ Row - 1, Col ] :: 0 ; true ), ( Row + Len =< Height -> Template [ Row + Len , Col ] :: 0 ; true ) -> true ; printf(error, "Crossfigure integrity violation adding " "a down figure of length %d,%n" "starting at (%d, %d)%n" , [ Len , Row , Col ]), abort ). % % init_matrix(Matrix, Template, Vars): % Finishes the initialisation of `Matrix', returning a list of all % the variables in it in `Vars'. % `Template' is used to determine which elements of `Matrix' should be % variables, and which ones should be blank. Blank elements are % filled with a ` '. % init_matrix( Matrix , Template , Vars ) :- dim( Matrix , [ Row , Col ]), ( for ( I , 1, Row ), fromto ([], Vars1 , Vars4 , Vars ), param ( Matrix , Template , Col ) do ( for ( J , 1, Col ), fromto ( Vars1 , Vars2 , Vars3 , Vars4 ), param ( Matrix , Template , I ) do T is Template [ I , J ], Elem is Matrix [ I , J ], ( var( T ) -> T = 0 ; true ), ( T = 0 -> Elem = ' ' , Vars3 = Vars2 ; Vars3 = [ Elem | Vars2 ] ) ) ). % % print_matrix(Matrix): % Prints `Matrix' in a readable format. % print_matrix( Matrix ) :- nl, ( foreacharg ( Row , Matrix ) do write( ' ' ), ( foreacharg ( Elem , Row ) do write( Elem ) ), nl ). %-------- Useful constraints for crossfigure puzzles --------% % % square(N): % Constrains N to be a square number. % square( N ) :- N #= T * T . % % prime(N): % Delays until N is ground, and then succeeds if and only if it is % prime. % prime( N ) :- ( nonvar( N ) -> is_prime_2(2, N ) ; suspend (prime( N ), 2, N ->inst) ). is_prime_2( Q , N ) :- N mod Q =\= 0, ( Q * Q < N -> Q1 is Q + 1, is_prime_2( Q1 , N ) ; true ). |