The Witch Puzzle I (witches)
Problem
: This puzzle is an interesting pastime. Print out the
in Figure 1 on a color printer.
3x3
puzzle squares shown
Cut out the nine squares to place the 9 pieces on the
table. The puzzle is simple: Arrange the pieces into a
pieces matches up so that each puz
3x3
square in such a way that the
front is matched up with the corresponding zle
horizontally and vertically with the next piece. Good luck! Depending on the puzzle and
how they are arranged, it's quite a challenge.
Great for a rainy day if your Stop the
Rain spell doesn't work! This puzzle is known as the witch puzzle because the pieces
of some commercial games contain bitmaps of colored witches.
Figure 1: A Witch Puzzle
How would you solve such a problem? By trial-and-error! No? After a while you'll
get used of the gures and how they are related to each other, so you may nd a solution
or not and you give up! This trial-and-error method has two important disadvantages:
(1) you do not know how long it will take, until you nd a solution, and (2) if you get
other puzzles with dierent bitmaps, you need to begin the process of getting into it
again. We would like to have a systematical method to solve all such kind of problems.
This is only possible, if we make mental eorts and
extract the essential from the problem,
this is what we mean by modeling. We formulate a mathematical model!
The rst approach could be to write a computer program, that enumerates all possible
arrangements of the blocks (pieces). There are (in a
1
3x3
puzzle)
9! = 362880
possibilities
to place the blocks at a particular position within the 3x3 square. Furthermore, each
9
block can be turned in 4 rotations at each position, which are 4 = 262144 ways. Hence,
9
0
0
0
we have 9! · 4 = 95 126 814 720 possible arrangement for this puzzle. If a computer could
9
test 10 arrangements per second, then a program would take 95 secs to enumerate all
possibilities. In a
4x4
puzzle, however, the same program would take more than 1 billion
of years to enumerate all arrangements, which are:
16! · 416 = 890 8620 6980 3100 0390 5020 8480 000
.
Of course, we do not need to check
all
arrangements. We can begin with a sample
and hope that a solution is found quickly. Furthermore, enumerating the possibilities in
a intelligent way avoids to check all of them.
It is also possible to use mathematical modeling techniques to nd a way to solve this
problem. And here we show how!
Modeling Steps
We begin to
name
explicitly all kind of
known things
in relation with this puzzle. At rst
this seems to be a trivial and boring task. However, it is absolutely necessary to order the
ideas and to connect them using a certain formal and precise language. First we have
dierent blocks (pieces) and we begin to assign the numbers
9
1 to 9 to them (see Figure 2).
We mark the numbers in the right-bottom corner of each block. It is not of importance
how the are distributed over the blocks, the important thing is that each block is labeled
with a dierent number.
9 position within the resulting 3x3 square. We do
of rows and columns (i ∈ {1, . . . , 3} and j ∈ {1, . . . , 3}), ordering them from
and from top to bottom. Then each position is a (i, j) combination with
The next step is to identify the
this by a set
left to right
1 ≤ i, j ≤ 3.
Next we dene the 4 possible rotations. Suppose all blocks in Figure 2 are dened to
0o . We can turn them counter-clockwise by 90o , by 180o , and by 270o , to get
have rotation
the four dierent possibilities of matchings. Let us dene them with a set
For block number
1,
d ∈ {1, . . . , 4}.
Figure 3 shows the four dierent rotations. Remember, the rotation
is counter-clockwise.
Next we identify the dierent bitmaps on the blocks. Each block contains at each
side one half of a gure. We assign them a number to identify them. There are
halfs, in such a way that they can be completed to
4
8 dierent
dierent gures. The assignment of
the number can be seen in Figure 4.
The assignment of the
8
numbers to the bitmaps is done in a way that the sum of the
two complementary partial gures is always
9,
for example, Bitmap 1 and Bitmap 8 (the
rst once), can be completed to a gure, and their sum is
9.
This can be veried for all
4 gures. The reason for choosing the number in this way will be claried below.
So far we did nothing else than identifying the elements, numbering and classifying
them; boring activities, which seems to have nothing to do with creative mathematics!
But we prepared the elements in a way to use them now. It is important and rewarding
to stay with this boring activity until we have all elements in the right way: to have
the right nomenclature, the right elements, names etc.
We have identied the
9
blocks, the
4
rotations, the
9
positions, and the
8
partial
bitmaps. We now begin to compose the elements: Each block in a particular puzzle 2
Figure 2: witch: Labeling the Blocks
contains
4
bitmaps. The rst block (BL1), for example contains the 4 bitmaps
(3, 6, 6, 1)
beginning with the top in clockwise direction. Hence, each blocks can be specied by four
numbers, the four bitmaps on it. This information can be collected in a table (see Table
1).
BLOCKS
BL1
BL2
BL3
BL4
BL5
BL6
BL7
BL8
BL9
top
3
2
6
7
2
3
2
8
2
right
6
4
4
7
5
7
6
5
8
bottom
6
8
4
1
5
5
5
3
8
left
1
7
8
1
1
1
3
4
2
Table 1: The Puzzle's Block Data
For all these elements we introduce now mathematical symbols:
1. The blocks are denoted by a set
k ∈ K = {1 . . . 9}
2. The positions are denominated by a tuple list as follows:
T = {(i, j)|i ∈ I, j ∈ I}
3
with
I = {1 . . . 3}
Figure 3: witch: Labeling the Rotations
3. The rotations are formalized by the set
d ∈ D = {1 . . . 4}
4. Finally, the data table of the bitmaps number for each blocks is generalized by
ad,k
d ∈ D, k ∈ K . A particular number ad,k denotes the bitmap on the top
k when the block is rotated counter-clockwise by d. For example, a2,2 = 4,
0
block 2 contains the top bitmap 4, if the block is rotated 90 counter-
with
of block
because
clockwise.
This previous step, to introduce algebraic symbols, has the advantage that we are no
longer talking about a
particular
puzzle, but about a whole class of similar puzzles. We
can easily dene a new puzzle just by replacing the numbers in the table
ad,k , for example.
Where are we now? What are we looking for? As far as we can see, we got all the
data for the problem. Now we want to know
and in
what rotation
which block
should be put in
which position
in order to match all bitmaps horizontally and vertically.
Hence,
k ∈ K is at position (i, j) ∈ T in rotation d ∈ D or
not. For each combination (i, j, k, d) we introduce a binary variable xi,j,k,d which is 1 if
the block k is at position (i, j) in rotation d, and 0 otherwise.
we want to know whether the block
The constraints are as follows:
1. At each position
(i, j),
we must have exactly one block
X
xi,j,k,d = 1
for all
k
in rotation
d,
hence:
(i, j) ∈ I × I
k,d
2. Eack block
hence:
k
must be put at a particular position
X
xi,j,k,d = 1
i,j,d
4
for all
(i, j)
k∈K
in a particular rotation
d,
Bi
t
maps8-1
Bi
t
maps7-2
Bi
t
maps6-3
Bi
t
maps5-4
Figure 4: witch: Labeling the Bitmap Figures
3. Horizontally and vertically, the two complementary bitmaps must match. How can
we full these requirements? Let's rst see, what the requirements mean: Suppose,
that we have a block with a bitmap
Then in position
(1, 2)
9
(at the right of
at the right edge in a position, say
(1, 1).
(1, 1)) we must place a block so that at the
1 (see Figure 5). This match must be
left edge of this block we get the bitmap
(1,2)
(1,1)
8
1
Figure 5: witch: Matching Bitmaps
enforced for all transitions from position
and
1 ≤ j ≤ 2,
(i, j)
and
with
1≤i≤3
this is the horizontal matching. It must also be enforced for the
vertical matching for all transitions from position
1≤i≤2
(i, j + 1)
to position
(i, j)
to position
(i + 1, j)
with
1 ≤ j ≤ 3.
4. For each horizontal transition, we must have:
X
ad mod 4+1,k · xi,j,k,d +
k,d
X
a(d+2) mod 4+1,k · xi,j+1,k,d = 9
k,d
for all
(Note that
ad mod 4+1,k
is the right bitmap of block
bitmap when the block is rotated by
d
5
k
and
i ∈ I, j ∈ I \ {3}
a(d+2) mod 4+1,k
in clockwise direction.)
is the left
5. For each vertical transition, we have:
X
a(d+1) mod 4+1,k · xi,j,k,d +
k,d
X
a(d+3) mod 4+1,k · xi+1,j,k,d = 9
k,d
for all
i ∈ I \ {3}, j ∈ I
Now, we also see why we choose to number the bitmaps in such a way that their complement sum is
9:
The matching two halves of each bitmap have assigned numbers such that
there sum is always
9!
Of course, this is an arbitray decision. We could have assigned
other numbers
The complete model code in LPL for this model is as follows (see [1]):
Listing 1: The Model
model witches "The Witch Puzzle I";
set i,j := [1..3] "The rows/columns";
d
:= [1..4] "The 4 rotations";
k
:= [1..9] "The 9 puzzle blocks";
integer parameter a{d,k} "The bitmap data"
:= [3 2 6 7 2 3 2 8 2
6 4 4 7 5 7 6 5 8
6 8 4 1 5 5 5 3 8
1 7 8 1 1 1 3 4 2];
/* integer parameter a{d,k} "The bitmap data"
:= [2 2 3 7 8 5 5 8 8
1 4 7 6 4 2 6 5 6
7 6 8 4 3 4 2 4 2
6 8 4 1 5 8 1 2 3]; */
binary variable x{i,j,k,d}
"Block k is at position (i,j) with rotation d?";
constraint
P{i,j}: sum{k,d} x = 1 "At each (i,j) is a block k";
Q{k}:
sum{i,j,d} x = 1 "Each block is at a position";
H{i,j|j<#j}: sum{k,d} a[d%4+1,k]*x[i,j,k,d]
+ sum{k,d} a[(d+2)%4+1,k]*x[i,j+1,k,d] = 9;
V{i,j|i<#i}: sum{k,d} a[(d+1)%4+1,k]*x[i,j,k,d]
+ sum{k,d} a[(d+3)%4+1,k]*x[i+1,j,k,d] = 9;
maximize obj: x[1,1,9,2] ;
Write{i,j,k,d|x}(’Place the block %1s at (%1s,%1s) with rotation %1s\
n’, k,i,j,d);
end
Solution
: The model prints the solution as follows:
Place
Place
Place
Place
Place
Place
Place
Place
Place
the
the
the
the
the
the
the
the
the
block
block
block
block
block
block
block
block
block
9
4
7
6
3
1
2
5
8
at
at
at
at
at
at
at
at
at
(1,1)
(1,2)
(1,3)
(2,1)
(2,2)
(2,3)
(3,1)
(3,2)
(3,3)
with
with
with
with
with
with
with
with
with
rotation
rotation
rotation
rotation
rotation
rotation
rotation
rotation
rotation
6
2
1
2
2
4
2
3
2
1
7
5
1
4
8
Figure 6: The Solution of the Witch Puzzle
This solution corresponds to the layout of the blocks that is given in Figure 6.
block
9
is at position
(1, 1)
For a new puzzle, we only need to specify the data table
Question
For example,
o
rotated by 90 counter-clockwise (rotation 2).
ak,d .
(Answer see )
1. Figure 7 shown another puzzle. Solve it!
2. Modify the table
ad,k
to
a{d,k} := [2
3
9
6
2
4
8
7
1
4
7
6
1
2
7
8
2
1
7
8
7
2
4
9
3
9
6
1
3
8
6
1
6
7
1
2];
What is the solution? Verify the solution!
3. Generate your own puzzle! In particular, generate a
Answer
4 × 4-puzzle
and solve it.
(Question see )
1. You need to enumerate the blocks, to assign the numbers to the bitmaps and specify the
table
ad,k .
Then one can use the model specied.
2. The solution is:
7
Figure 7: Another Witch Puzzle
Place
Place
Place
Place
Place
Place
Place
Place
Place
the
the
the
the
the
the
the
the
the
block
block
block
block
block
block
block
block
block
8
2
9
6
1
4
3
7
5
at
at
at
at
at
at
at
at
at
(1,1)
(1,2)
(1,3)
(2,1)
(2,2)
(2,3)
(3,1)
(3,2)
(3,3)
with
with
with
with
with
with
with
with
with
rotation
rotation
rotation
rotation
rotation
rotation
rotation
rotation
rotation
4
1
2
2
1
4
1
4
4
To check we need to verify if all transitions sum up to 9.
1 (see next section).
3. The answer is given in model witches1
Alternative Formulations
The two constraints
k
H
is placed in rotation
and
d,
V
can be formulated in dierent ways: If at a position
then at the position to the right (or below) [(i, j
+ 1)
(i, j) a block
(i + 1, j)] a
or
block must be placed in such a way that the two corresponding bitmaps matches. This can be
coded in LPL as follows:
1 http://lpl.unifr.ch/lpl/Solver.jsp?name=/witches1
8
H{i,j,k,d|j<#j}: x -> or{k1 in k,d1 in d | k<>k1
and a[d%4+1,k]+a[(d1+2)%4+1,k1]=9} x[i,j+1,k1,d1];
V{i,j,k,d|i<#i}: x -> or{k1 in k,d1 in d | k<>k1
and a[(d+1)%4+1,k]+a[(d1+3)%4+1,k1]=9} x[i+1,j,k1,d1];
(i, j) a block k is placed in rotation
(i + 1, j)] a block must not be placed
Still another formulation is as follows: If at a position
d,
then at the position to the right (or below) [(i, j
+ 1)
in such a way that the two corresponding bitmaps do
or
not match.
It seems that this is simply a
negation of the formulation above. However, it leads to a very dierent model containing much
more constraints as the second approach. In LPL, the formulation is as follows:
H{i,j,k,d,k1 in k,d1 in
a[(d1+2)%4+1,k1]<>9}:
V{i,j,k,d,k1 in k,d1 in
a[(d1+3)%4+1,k1]<>9}:
d|j<#j and
x[i,j,k,d]
d|i<#i and
x[i,j,k,d]
k<>k1 and a[d%4+1,k] +
+ x[i,j+1,k1,d1] <= 1;
k<>k1 and a[(d+1)%4+1,k] +
+ x[i+1,j,k1,d1] <= 1;
References
[1] T. Hürlimann.
Reference Manual for the LPL Modelling Language, most recent version.
www.virtual-optima.com.
9
© Copyright 2026 Paperzz