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).
In the world of pencil puzzles, there are many puzzle types where you have to draw a loop on a lattice grid, includingFormally, 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 square.
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.
It's difficult to apply the case by case basis on the easy version here, since there's no systematic approach. (The easy version uses the givens instead.) So how can we begin tackling this problem?
First, we have an interesting observation. Look at the grid points:
Each grid point is either inside the loop or outside the loop:
This already shows how we can tackle this with brute force: consider the 2 1 6 possibilities (two for each grid point, all multiplied together) one by one, and verify if it makes a loop. The problem is, how do we verify whether a selection of points make a loop? Additionally, is it possible that two loops produce the same grid points that are "in"?
The second question, that each configuration can only possibly produce one loop, is easier to answer. Note that the loop always goes between the inside part and the outside part (this is Jordan curve theorem ). Thus between two grid points of different "parity" (one "in" and one "out"), the loop must go there, and between two grid points of equal "parity" (both "in" or both "out"), the loop cannot go there. The outside of the grid also has grid points that are "out", so this defines the loop completely.
The first part is more difficult. Let's see what characteristics that a loop has.
First, there must be a single loop. This means all the "in" grid points together form a single region. In other words, if we attempt to use flood fill from an "in" point, we must find all "in" points.
Second, it cannot self-intersect. This means no square can be used four times. A square used four times means that the four grid points around it are colored in a checkerboard fashion:
We can actually simplify this assumption, which will also take care of another possibility. If we use the first condition that the "in" points must be connected, we will have something of this sort:
In other words, one of the two "out" grid points will eventually be sealed off from the outside. This also applies to another case that we missed previously, but clearly isn't allowable either:
In other words, if we flood fill from an "out" point, we must get another "out" point.
In fact, the two above are enough. A coloring of the grid points forms a loop if flood filling from any "in" point finds all "in" points, and flood filling from any "out" point finds all "out" points (if we assume the border is all "out" as well). Now onto the coding part. It's simple enough:
The function
size(G, x, y)
takes a gridG
and a coordinate(x, y)
, and uses flood fill to determine the size of the area containing it. The functioncheck(G)
begins by taking a gridG
, checking that it has an "in" point (a grid of "out" points is useless; there's no loop), and padding the borders with out points. It then finds the area with an "out" point, finds the area with an "in" point, and adds the two together, if it's the entire size of the grid, then this is a valid grid, otherwise it's invalid. The functionsolve(r, c)
takes two integersr, c
and returns the number of valid grids for r × c point grid; that is, ( r + 1 ) × ( c + 1 ) square grid.For our purpose, we need to find the number of solutions of 5 × 5 square grid, so we compute
solve(4, 4)
. The returned answer is 9 3 4 9 .