In the world of pencil puzzles, there are many puzzle types where you have to draw a loop on a lattice grid, including Country Road , Masyu , Pure Loop , Slalom , and Yajilin . In most of these puzzles (and all of the linked above), the loop visits some of the cells, passing through the cells' centers, and may not use a cell more than once (which also means no intersections, no touching itself, etc).
Formally, on a polyomino P , a loop is a sequence of n ≥ 4 squares ( a 1 , a 2 , a 3 , … , a n ) such that all squares a i are in P , a i and a i + 1 share a side for all valid i , a n and a 1 also share a side, and all squares in the loop are distinct. Loops are cyclic (it can start from any square in the loop) and don't have any orientation (reversing the loop doesn't matter), thus ( a 1 , a 2 , a 3 , a 4 ) , ( a 2 , a 3 , a 4 , a 1 ) , ( a 4 , a 3 , a 2 , a 1 ) all describe the same loop.
Determine the number of loops on a 3 × 3 0 rectangle.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
We consider the crossings of the grid in the interior of the rectangle. They form a rectangle of 29 x 2.
For each column of two crossings, there are only three possibilities: (0) both crossings are in the interior of the loop; (1) the top crossing is in the interior of the loop; (2) the bottom crossing is in the interior of the loop. The condition of connectivity prohibit (1) and (2) to occur next to each other. All other possibilities are allowed.
We count the possible patterns of n columns wide. However, instead of adding them all together, we separate them out based on the shape of the first column. Thus, s [ 5 ] [ 1 ] is the number of patterns that are five columns wide and start with a column with the top crossing in the interior of the loop. This allows us to move from width n to width n + 1 while respecting the rule that columns of types (1) and (2) may not be combined. (In the code below, this is the condition d + d d = 3 .)
Example: The 17 patterns of length 3 are counted as follows:
1 2 3 4 5 6 7 8 |
|
Finally, when placed in a rectangle of N = 2 9 columns, each pattern of n columns wide can start in N + 1 − n possible positions. We implement this in the last part, when determining the grand total of possible patterns.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
The output is the solution 4 4 3 3 6 5 5 4 4 3 8 7 .
Incidentally, running the program with N = 4 gives the solution to the "easy" loops-the-loops problem.
Problem Loading...
Note Loading...
Set Loading...
I computed values for 3 × n rectangles until n = 1 5 and found that it was always very close to 4 ( 2 + 1 ) n + 2 − 8 n − 6 . Pluging n = 3 0 in it gives 443'365'544'386.9993, rounded to 443'365'544'387.
Also, see OEIS A059020 .