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 , a loop is a sequence of squares such that all squares are in , and share a side for all valid , and 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 all describe the same loop.
Determine the number of loops on a 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.
So the solution is 8 9 4 3 3 6 0 9 0 6 6 7 5 3 7 8 1 8 3 8 .
Here is my code. However, the 'long' datatype is too small to contain the results for N > 27. I used two 'longs' for the variables s[][] and total, but that cluttered my code, so I left that out here.
My method is as follows: s[n][x] keeps track of the number of patterns of length n starting with column of type x. The values of x are defined as follows:
x = 0, 1, 2: The loop includes the top, middle, bottom 1/3 of the column.
x = 3, 4: The loop includes the top, bottom 2/3 of the column.
x = 5: The loop includes the entire column.
x = 6, 7: The loop includes both the top and the bottom of the column. The difference is, that x = 6 if the pattern is closed toward the right, and x = 7 if the pattern is not closed.
Patterns starting with x = 6 are valid, but run the risk of including "islands", which is not allowed in a loop. Patterns starting with x = 7 are not (yet) valid because the top and bottom are disconnect, but they may be used to grow longer patterns. In the final total, we exclude the patterns with x = 7.
The array stitch[x from][x to] describes which kinds of column may be legitimately combined with each other. The array is symmetric except for the last two rows/columns, which are designed to prevent both islands and disconnected shapes.