Вы находитесь на странице: 1из 10

Loop the Loop.

Puzzle Definition.
To understand the puzzles please refer the image below (Courtesy Times of India). I
hope that would be self-explanatory.

Image 1
The solution:
Image 2

Basically you have to mark every edge around a cell (The Square formed by any four
dots), with a cross or a line that would be part of the loop. The problem ends when all the
edges of each cell are marked by a line or cross, obviously by confirming to the rules.
The algorithm does not do much different from what you would do as an approach to the
puzzle. There are few terminologies that must be known before we go further.
This algorithm is designed only to solve such puzzles. As it cannot generate any
puzzles on its own, puzzles used from external sources should be used (I’ve used Times
of India). It would give only the solution to it.

Terminologies:
Cell naming convention: As you can see there are seven rows and seven columns of
cells, for addressing the cells I’ve used the following convention, it’s how a two
dimensional array is best represented.

Image 3
The numbers at the left of the puzzle give the row numbers and the numbers to the top
give the column of the cells. So, the cell at row 1 and column would have 2 lines around
it (value 2), similarly the cell at 3rd row and 4th column would have 1 line around it. Such
point of view where the puzzle is looked as cell patterns is called the ‘Cell Space’.
Points naming convention: It is necessary to identify the points that form the
individual cells for reasons explained later. Points are also represented as rows and
columns which would be represented by a distinct array from the cells. The following
convention would be used. (Notice the numbers are standing besides the dots now, not
against the cells.)

Image 4
So, the point in row 1 and column 2(i.e. point at (1, 2)) is the top left corner of cell (1,
2) which has value 2 in it. Such point of view where the puzzle is looked as point patterns
is called the ‘Point Space’. We would be using both the point space and cell space to get
the solution as explained in later sections.

C doesn’t have any array bounds check, so we have to do few things to make sure there
are there are no array over flows or in layman terms we must follow a rule saying there
would exists no line out of the puzzle. Look at the following image for clarity.
Image 5

The difference is that now the puzzle has extra rows above and below and extra
columns on left and right of the puzzle, and every cell in the extra row, column is marked
X on its edges (in red) which are not part of the puzzle, so that any line in the puzzle
won’t extend its boundaries. Its use is not only limited to sealing which would be clearer
once we get into the logic of the program. Let us call this step ‘Boundary Sealing’.
Note: - There would be no additional numbers in the point space to represent the extra
points formed by boundary sealing, because even if there are additional points added in
the point space, they wouldn’t be used.

The point where no line meets is called a ‘black dot’ i.e. a point is surrounded by all
Xs. For e.g. the points (3, 4) and (5, 2) in the fig (a) in the first image where the puzzle is
described, are black dots. It is not necessary to have 4 Xs around the point to make it a
black dot, only three Xs are enough. Consider the following part of the puzzle

Image 6
Point (8, 1) in the image above has three Xs surrounding it. Even if a line can be
possible as drawn, the line wouldn’t extend anywhere as the point is all surrounded by Xs
in the remaining edges around the point, and this is violation of loop where no line can
have an open end in the solution at last. Hence if three Xs are marked around a point the
remaining edge can also be marked X, like in the following image.

Image 7
Point (8, 1) hence, is a black dot. The reason why the corner point (8, 1) was taken as
example is that it also demonstrates how boundary sealing is helping in determination of
a black dot. The 2 edges which are marked X in red (part of boundary sealing), help form
the three Xs around the corner point and thus render the point (8, 1) a black dot.

Image 8
Let us look into the rule ‘there would be no open ends of any lines in the final solution’.
Point (5, 7) and (7, 6) are open ends of their respective lines in the partial solution. Let us
call these points ‘Active Points’. These active points cannot exist in the final solution as
they are, hence another line must be drawn from these lines. Complying with the rule,
there would a line from point (5, 7) to its top and from point (7, 6) to its left as there
would not be any other line possible due to the existing Xs as explained in the following
image.

Image 9
Once this happens these points are now ‘Deactivated’ and points (4, 7) and (7, 5) are
‘activated’. Further we would deactivate these new active points using the same logic.
This would happen till we get the final loop where there would be no active points
remaining.

Image 10

As seen in the above image (left) point (6, 1) is an active point. By rule it should have a
line drawn from it. Two lines are possible from the point; one to the top and other to the
right. If, as seen in the image (middle), a line is drawn to the right it would form what is
called an ‘Internal Loop’ i.e. a loop inside the solution. As a rule only one loop can exist
which would be in form of the solution and no other loops can be formed. Hence this
edge would be marked X instead of line and now the active point (6, 1) would have a line
to its top, thereby deactivating it. Let us call this step ‘Elimination of Internal Loop’.
These ideas of naming conventions, boundary sealing, black dots, internal loops and
active points would be implemented in some part of the algorithm. No need to learn these
just keep the central idea in mind.

Data structure and Algorithm

Two important data structures that are used:

1) Point space:
The point space is represented by a two dimensional character array of size 8x8 named
ltlp (loop the loop point) to represent the 64 points that are present in the puzzle. The data
structure would be having zero stored initially and character 'A' if the point is active and
character 'D' if it is deactivated after. For example if the point ltlp [7][8] has value or is
assigned value 'A' it is an active point which should be deactivated. (Note: - Actually, the
size of the array used in the program is 9x9 because numbers in the range 1-8 have been
used to address the points neglecting 0, and also the extra points that are formed by
boundary sealing are not considered because they do not make any difference).

2) Cell Space:
The cell space is represented by a three dimensional character array of size 9x9x6
named ltl (loop the loop). The dimension formed by 9x9 represents the individual cells
(in range of 0-8 inclusive of the extra cells formed by boundary sealing). Every cell has 6
spaces in the third dimension (range 0-5) and they individually represent:
a) The edges (0: left 1: top 2: right 3: bottom): Every cell has four edges around it. The
statuses of these edges are represented by the characters from 0-3 in the third
dimension for every cell. 0 stands for the left edge 1 for top edge 2 for right edge
and 3 for bottom edge. These spaces are assigned character 'X' if the point is
marked X or 'l' if a line is marked on that edge, no other value would be assigned
(except the initial value 0). For example if ltl [7][8][0] has value or has been
assigned value 'X' it means that the top of cell (7, 8) has been marked X.
b) The number in that cell (4): This space in the third dimension represents the number
if present inside the cell. This is where the actual puzzle is loaded. It has values
from 0-3 representing possible number of lines around that cell and 5 if the cell is
empty. For e.g. if ltl[5][7][4] has value 0 then no line can be drawn around it,
similarly if ltl[7][6][4] has value 5 then there was no number used in cell (7, 6) in
the puzzle and there can be any number of lines possible around it (obviously
between 0 and 3).
c) Cell completion (5): This space denotes if all the edges around the cell have been
marked according to the number contained in that cell. This is used just to speed up
the process and obviously won’t be used in case the number is 5.

Algorithm
Data structure
2 trigger types a) active point b) 0 and3 c) actual marking

Algorithm:
Finally we are at the step where the actual processing starts. First we need to record the
puzzle into the data structure that we have designed for the cell space, as the puzzle only
contains numbers inside cells. As explained earlier the fifth character in the third
dimension would be used to store the numbers against every cell. This can be achieved by
using nested loops scanning value for cells row wise. A value of 5 would be entered by
the user where the value isn’t present inside the cell.

Once we get the puzzle in form of the data structure (in the form where the code could
use it), we would seal the boundaries before getting into solving the puzzle for the
reasons explained above. This can be done in the following way: (Refer image 5)
a) All the cells in the row 0 have their top, left and right edges marked X.
b) All the cells in column 0 have their left, top and bottom edges marked X
c) All the cells in row 8 have their left, right and bottom edges marked X
d) All the cells in column 8 have their top, right and bottom edges marked X.

Once these two initial steps are done with, we begin solving the puzzle. What would be
the triggering point for the puzzle? As we have seen in the description of the problem in
image 1, we would begin with the cells which are numbered 0 in the puzzle. We would
mark every edge around it X.
This is implemented by a function called ‘check’. This function takes the position of the
cell in from of row and column number checks if all edges can be marked according to
the number present inside it. Suppose if a cell (7, 1) has number 1 written inside it and if
3 edges have been marked X due to some reason then the remaining single edge of the 4
edges around that cell would obviously have a line (as there are only four edges in a cell)
and would be marked ‘l’ or vice versa. This logic would naturally work with cell having
the number 0, which means no edge would have ‘l’, hence they all would be marked ‘X’.
It does the following steps. Assume that the row and column passed is r and c.
a) Check if the number inside the cell has value 5 (stored in ltl[r][c][4]). If so, it is
uncertain that how many lines/Xs can be drawn around that cell. Exit processing
here else go to step b).
b) Count the numbers of lines (‘l’) and Xs present around each cell in variables
countx and countl.
c) The number of possible lines is what the number inside the cell gives. If this
number inside the cell (ltl[r][c][4]) equals countl then the remaining edges of
the cell can be marked X.
d) The number of possible Xs would be given by the 4 minus number inside the
cell. If the number of possible Xs equal countx, then the remaining edges of the
cell can be marked ‘l’ (i.e. line can be drawn).

Whenever I talk of marking a line series of steps are involved in doing that. These all
are done by the function cancel_all. This function takes in the row (int row), column (int
col) of the cell and the value (char val) to be marked (X/l) and the position of the edge
(int pos) inside the cell (left/top/bottom/right).
First, I’ll try to explain what the algorithm does in general and then I’ll discuss these
ideas with respect to the position of the edge.
Step 1: If you look closely on the way the data structure to represent the cells is made,
you’ll notice that the cells edges are shared with their neighboring cells. For example

Image 11
In the image 11 cell (1, 2) has its left edge is same as the right of cell (1, 1). Similarly
the top edge of cell (1, 2) is shared with cell(0, 1)‘s bottom edge. This is because
irrespective of its position every cell has four edges surrounding it (left, top, right,
bottom). So in general:
a) If a cell’s left edge is marked X/l, the right edge of the cell to its left should be
marked the same.
b) If a cell’s right edge is marked, the left edge of the cell to its right should be
marked.
c) If a cell’s top edge is marked, the bottom edge of the cell to its top should be
marked.
d) If a cell’s bottom edge is marked, the top edge of the cell to its bottom should be
marked.

Step 2: This step is where the setting of points active in the point is done. As explained
earlier if a line is drawn between any two points then those two points must be marked
earlier.

Image 12
In image 12 the numbers in blue represent the numbering of the points and those in
black give the numbering for the cells. As can be seen, for example, when the top edge of
cell (1, 2) is marked with a line (l) then the point (1, 2) and point (1, 3) should be marked
active. Similarly if bottom edge is marked with a line then point (2, 2) and point (2, 3)
should be set active. In general for a cell (r, c):
a) If a top edge is marked l then point (r, c) and point (r, c+1) should be assigned value
‘A’ (Activated).
b) If a bottom edge is marked l then the point (r+1, c) and point (r+1, c+1) should be
value ‘A’ (Activated).
c) If a right edge is marked l then the point (r, c+1) and point (r+1, c+1) should be
assigned value ‘A’ (Activated).
d) If a left edge is marked l then the point (r, c) and point (r+1, c) should be assigned
value ‘A’ (Activated).
And also those points that have already been deactivated should not be activated again.
Now a question is that when a point that is activated would get deactivated?
Image 13
In image 12 the line to the left once drawn would deactivate the point (1, 1) because the
top edge is already marked l and point (2, 1) because the bottom edge is also marked l.

Image 14

In any case when a line to a left edge or a right edge is drawn like right edge of cell (2, 1)
or left edge of cell (2, 2) in image 14 then total 3 lines should be checked for existence to
deactivate the top point and if any exists the top point (point (2, 2) in this case) should be
deactivated, similarly 3 for bottom point (point (3, 2) in this case).
In case when a line to a top edge or a bottom edge is drawn like bottom edge of cell (1, 2)
or top edge of cell (2, 2) in image 14 then total 3 lines should be checked for existence to
deactivate the left point and if any exists the left point (point (2, 2) in this case) should be
deactivated, similarly 3 for right point (point (2, 3) in this case).

Вам также может понравиться