Escolar Documentos
Profissional Documentos
Cultura Documentos
0
Due Friday April 1st, 2016
A
(pronounced higher-I.Q.) puzzle is an array of 33 black or white
pixels (bits), organized in 7 rows, 4 of which contain 3 pixels each and the middle 3 rows
contain 7 pixels each. You are given a particular configuration of these pixels as in (A).
(A)
(B)
Your task is to write a JAVA program that will transform this configuration into the solved
configuration (B). To modify your configuration you are allowed to apply one of two
substitution rules whenever the rule is applicable: anywhere in your configuration if one
of these patterns is found (horizontally or vertically), you can replace it with the other:
We recommend that you use a tree/graph data structure to represent each reachable
configuration of the
puzzle. The children of a configuration should be the
possible configurations that can be reached applying a single substitution rule. Your
algorithm should traverse the tree so to locate the solved configuration. Once the solution
is found, your algorithm should return a String containing the sequence of substitutions
necessary to solve the puzzle. For convenience we will name the BBW to WWB
substitution a B-substitution and the WWB to BBW substitution a W-substitution.
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
board:
For instance 8W10 means a W-substitution may be applied to pixels at positions 8,9,10.
Your output should be something like : 18W16, 5B17,, 16W28. We will provide you
with an Object type HiRiQ that you must use for uniformity reasons. This object will
contain methods for testing if the puzzled is solved, and outputting a board configuration.
This will be available through the course web page very soon.
In order to make your program (space) efficient, you must implement some sort of
memory management policy to avoid overflowing the virtual machine memory carelessly.
You will be evaluated on the ability of your program to solve various puzzles: maybe
your program cannot find a solution from ALL configurations, but maybe it can find a
solution for a configuration with only 20 white pixels on it, or only 10 white pixels,
Examples on various configurations to be solved will be provided by us on the course
web page as well. The more complex puzzles you can solve, the better grade you will get.
Note: if you choose to solve this problem without explicitly exploring a tree, you must
explain where a virtual tree is being explored and how it is explored (DFS, BFS, etc).
The standard HI-Q puzzle has a unique start-configuration (with 32 white pixels)
opposite to the solved state and only allows W-substitutions:
In a first example, we have the solved state with its four children :
In a second example, we have the configuration (A) from above with the horizontal
substitutions to get 6 of its children and the vertical substitutions to get its other 10 :
List of 38 triplets:
0
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
In general, when looking for the children of a given configuration, the following 38
triplets should be considered for applying the substitution rules:
[0,1,2]
[3,4,5]
[12,19,26]
[11,18,25]
[6,7,8]
[ 7, 8, 9 ]
[ 8 , 9 ,10]
[ 9 ,10,11]
[10,11,12]
[ 2 , 5 ,10]
[ 5 ,10,17]
[10,17,24]
[17,24,29]
[24,29,32]
[13,14,15]
[14,15,16]
[15,16,17]
[16,17,18]
[17,18,19]
[1,4,9]
[ 4 , 9 ,16]
[ 9 ,16,23]
[16,23,28]
[23,28,31]
[20,21,22]
[21,22,23]
[22,23,24]
[23,24,25]
[24,25,26]
[0,3,8]
[ 3 , 8 ,15]
[ 8 ,15,22]
[15,22,27]
[22,27,30]
[27,28,29]
[30,31,32]
[ 7 ,14,21]
[ 6 ,13,20]
For this assignment, sky is the limit. All sorts of improvements can be considered. You
must submit a working solution to get some points. Once you have a basic working
solution, it is up to you to improve it as much as you like. Here are a few ideas about
possible improvements:
To increase the number of solvable puzzles, first build a solution table of ALL
configurations with only a few white pixels. Use that table as soon as you reach a
configuration with few enough white pixels.
Are all configurations (except for the 16 with zero neighbours) reachable from the
solved state ? If not, can you rapidly identify those unsolvable configurations ??
If possible avoid using B-substitutions to converge faster towards the goal.
Some helper code is found below. 1) HiRiQ(n) creates a HiRiQ object with 4 possible
values for n=0,1,2,3. For an object conf of type HiRiQ, 2) conf.IsSolved() decides if it is
solved, 3)_conf.store(B) stores an array B of booleans in conf, while 4) conf.load(B)
returns B filled with booleans. Finally, 5) conf.print() prints conf s formatted contents.
(n=0)
(n=3)
Some
Analysis:
Consider the above 3-colouring of the puzzle cells. Every cell is either blue, yellow or
red. No two adjacent cells are of the same colour. It contains exactly 11 blue cells, 11
yellow cells and 11 red cells. All the allowed substitutions will flip exactly one pixel on a
blue cell, one pixel on a yellow cell and one pixel on a red cell. A-L-W-A-Y-S
Notice that the solved configuration contains one white pixel on a yellow cell, zero white
pixel on blue or red cells. We can write this as a triplet: ( 0 , 1 , 0 ). Whatever substitution
we apply will switch the number of white pixels on each of the three colours from even to
odd or the opposite: the parity (even / odd) of the number of white pixels on each type of
coloured cells will always be the same for blue and red and always opposite for yellow.
For example, starting from the solved state ( 0 , 1 , 0 ) it may be changed up to ( 1 , 0 , 1 )
and then back down to ( 0 , 1 , 0 ) or up to ( 2 , 1 , 0 ) or ( 0 , 1 , 2 ). The parities of blue
and red will always be the same and yellow the opposite. Therefore any configuration
with colour distribution ( B , Y , R ) with 0 B 11 white pixels on blue cells, 0 Y 11
white pixels on yellow cells and 0 R 11 white pixels on red cells must satisfy
Parity( B ) = B %2 = R %2 = Parity( R ) Parity( Y ) = Y %2
to be reachable from ( 0 , 1 , 0 ). However, it is a necessary but not a sufficient condition.
A number of conclusions can be deduced from this observation:
Any configuration with a single white pixel must be of the form ( 0 , 1 , 0 ). Thus a
single white pixel must always be on a yellow cell to be reachable :
But by symmetry, the same can be argued about the (horizontally or vertically) flipped 3coloured board. So single white pixels must also be on a yellow cell of the flipped board:
Only 5 cells (marked by stars) are yellow in both the original colouring and the flipped
one. These are the only 5 configurations with a single white pixel that are reachable from
the solved state.
By the same argument, any configuration with 32 white pixels must be of the form
( 11 , 10 , 11 ). Thus the single black pixel must be on a starred yellow cell as well. All
5 such configurations are reachable and no more.
Any state with 2 white (/ 2 black) pixels must be of type ( 1 , 0 , 1 ) (/ ( 10 , 11 , 10 )).
Thus the 2 white/black pixels must be on a blue and a red cell. This is also true if we
flip the board vertically, horizontally or both. This is possible in only a few places. The
yellow cells below indicate cells that are yellow in one of the (flipped) boards.
16
28
29
30
31
32
( 1 , 4 , 11 )
( 3 , 2 , 11 )
( 5 , 0 , 11 )
(1,6,9)
(3,4,9)
(5,2,9)
(1,8,7)
(3,6,7)
(5,4,7)
( 1 , 10 , 5 )
(3,8,5)
(5,6,5)
(7,0,9)
(9,0,7)
( 11 , 0 , 5 )
(7,2,7)
(9,2,5)
( 11 , 2 , 3 )
(7,4,5)
(9,4,3)
( 11 , 4 , 1 )
(7,6,3)
(9,6,1)
( 9 , 10 , 9 )
( 11 , 8 , 9 )
( 11 , 10 , 7 )
( 7 , 10 , 11 )
( 9 , 8 , 11 )
( 11 , 6 , 11 )
( 8 , 11 , 10 )
( 10 , 9 , 10 )
( 9 , 10 , 11 )
( 11 , 8 , 11 )
( 10 , 11 , 10 )
( 11 , 10 , 11 )
( 10 , 11 , 8 )
( 11 , 10 , 9 )
( 3 , 10 , 3 )
(5,8,3)
( 5 , 10 , 1 )
(7,8,1)