Welcome to Open Problem #4! This is the first problem that's been drawn from our suggestion thread. This problem comes from Andrew Hayes. I want to draw from that thread more, so please, if you have any ideas for future open problems, send them in!
Consider an by grid. Each square can either be open or closed, except for the top left and bottom right squares, which are always open. How many arrangements exist such that there exists a path from the top left to the bottom right square (moving only up/down/left/right)?
Computer science mavens may recognize this as related to the "Rat in a Maze" problem used in textbooks. In the traditional setup, you are given the maze and then asked how many solutions are there. The textbook question is then to write a backtracking algorithm that finds all solutions to the maze.
This page has source code in both C++ and Java to the traditional problem. (Note: external link.)
The question posed here is very different, and as far as I can find in my comprehensive search, has never been asked. If we are given a particular size of grid, how many unique "mazes" can we make? Note we could adapt existing source code (like the example above) to work on solving this by simply adding a loop that tests every single maze configuration for a particular size. This is actually a fairly good way to start the problem.
However, we'd like to go a little farther, and start finding mathematical patterns. One approach might be to only ask about how many "mazes of a particular type" there might be; so not a comprehensive counting, but using some sort of generating rule to make mazes that are guaranteed to work and where it's easy to count how many mazes come from the rule.
As a goal for this open question, I'd like to get enough information that we can make the start of an entry in the On-Line Encyclopedia of Integer Sequences. (That is, we can solve it for 2 by 2, 3 by 3, 4 by 4 etc. up to a certain point - even if we don't have a general formula, we can calculate as many numbers as possible in the sequence.)
Other news:
For Open Problem #3, if you haven't read it yet I definitely recommend David Vreken's solution here. He has two more example figures and mentions a possible new direction for research (using shapes other than isosceles right triangles for the "folding" method).
I will be putting the paper from Open Problem #2 up on ArXiv fairly soon, and sending it somewhere for publication (I'm guessing by the end of this week).
Easy Math Editor
This discussion board is a place to discuss our Daily Challenges and the math and science related to those challenges. Explanations are more than just a solution — they should explain the steps and thinking strategies that you used to obtain the solution. Comments should further the discussion of math and science.
When posting on Brilliant:
*italics*
or_italics_
**bold**
or__bold__
paragraph 1
paragraph 2
[example link](https://brilliant.org)
> This is a quote
\(
...\)
or\[
...\]
to ensure proper formatting.2 \times 3
2^{34}
a_{i-1}
\frac{2}{3}
\sqrt{2}
\sum_{i=1}^3
\sin \theta
\boxed{123}
Comments
In addition to some of the suggestions already given in this thread, I also found it helpful to consider each maze possibility by categorizing them by the exact number of black and white squares.
A 2 x 2 grid has 22=4 total squares, and subtracting the start and finish squares gives a total of 22−2=2 squares that can be either black or white (the top right corner and the bottom left corner). Therefore, a 2 x 2 maze can have exactly 0, 1, or 2 black squares. There is (02)=1 way to have a 2 x 2 maze with exactly 0 black squares, which has a path. There are (12)=2 ways to have a 2 x 2 maze with exactly 1 black square, and since 1 square is not enough to block a path, all of these have paths. There is (22)=1 way to have a 2 x 2 maze with exactly 2 black squares, and this does not have a path. Therefore, out of the total 22=4 possible 2 x 2 mazes, 3 have paths and 1 does not.
The results of all 2 x 2 mazes are summarized below:
A 3 x 3 grid has 32=9 total squares, and subtracting the start and finish squares gives a total of 32−2=7 squares that can be either black or white.
There is (07)=1 way to have a 3 x 3 maze with exactly 0 black squares, which has a path.
There are (17)=7 ways to have a 3 x 3 maze with exactly 1 black square, and since 1 square is not enough to block a path, all of those have paths.
There are (27)=21 ways to have a 3 x 3 maze with exactly 2 black squares, but there are only 2 ways to block a path with just 2 black squares - one to the right and one below the starting square, or one to the left and one above the finish square. This leaves 21−2=19 possible mazes with paths.
There are (37)=35 ways to have a 3 x 3 maze with exactly 3 black squares, and analyzing this one is tougher. First of all, a path can be blocked with just 2 squares in 2 ways (as mentioned above), and there are 5 other places to put the third square, for a total of 2⋅5=10 possible mazes without paths.
Secondly, a path can be blocked with exactly 3 squares (unique to the ones already mentioned) by making barriers from one side to an opposite corner in 4 ways, from one side to an opposite side in 2 ways, and from one corner to the other corner in 1 way, for 7 more ways.
Therefore, out of the total 35 possible 3 x 3 mazes with exactly 3 black squares, 10+7=17 do not have paths, meaning 35−17=18 do have paths.
There are (47)=35 ways to have a 3 x 3 maze with exactly 4 black squares. If the maze has 4 black squares, then there must be 7−4=3 white squares, and there are only 6 ways of making a path with exactly 3 white squares - 4 that pass through the middle square, 1 that passes through the top right corner square, and 1 that passes through the bottom left corner square. Therefore, out of the total 35 possible 3 x 3 mazes with exactly 4 black squares, 6 have paths, meaning 35−6=21 do not.
There are (57)=21 ways to have a 3 x 3 maze with exactly 5 black squares. If the maze has 5 black squares, then there must be 7−5=2 white squares, which is not enough to complete a path from the starting square to the finishing square. Therefore all 21 possible 3 x 3 mazes with exactly 5 black squares do not have paths.
By the same reasoning, all (67)=7 ways of having a 3 x 3 maze with exactly 6 black squares do not have possible paths
and the (77)=1 way to have a 3 x 3 maze with exactly 7 black squares also does not have a possible path.
Therefore, out of the total 27=128 possible 3 x 3 mazes, 51 have paths and 77 do not. The results are summarized below:
As we move to larger mazes, the calculations needed become more and more complex. However, for now we can make a few generalizations:
For an n x n maze, there are n2−2 squares that can be either black or white, and therefore 2n2−2 total possible mazes.
All mazes need at least 2 black squares to block a path. Therefore, the number of mazes with exactly 0 and 1 black squares always give paths. So the number p of n x n mazes with paths is ∑k=01(kn2−2)<p<2n2−2.
All n x n mazes need at least n−1 white squares in a path to go to the right side of the maze, and another n−2 white squares in a path to go down to the bottom of the maze, for a total of at least (n−1)+(n−2)=2n−3 squares. If it has 2n−4 or less white squares, a path cannot be made. Therefore, the number q of n x n mazes without paths is ∑k=02n−4(kn2−2)<q<2n2−2.
This could be a possible approach:
If we modify the rules to allow diagonal movements (movement along the diagonal), then if and only if the closed squares form a path from one side of the square to another, and passes by the top-left square, then there are no solutions to the maze.
Paths that the closed squares can form such that there are no solutions to the maze. These will be called barricades:
Example of such a path:
EDIT: As highlighted by Carl below, the significant advantage to this approach is that barricades for a smaller grid are also barricades for a larger grid. Perhaps a similar approach to Stefan (below), to consider a small grid of dimensions Rx1 and adding more collumns, can suffice.
Log in to reply
In your image if you add another row and column can you not just go around the barrier? Carl's point would only apply of you block both vertical and horizontal movement.
It might be easier to think of this in terms of the complement: How many arrangements do not have such a path? A grid will lack a path from corner to corner only if there is a barrier, a set of closed squares that completely encloses your starting point. A possible advantage of thinking in these terms is that any barrier on an order n grid is also a barrier on an order n+1 grid.
NOTE: I missed 18 squares. Check Stephan VDW's reply to this for the full list
A comprehensive list of non-paths for a 3 x 3 maze (I think). ■ represents the entrance/exit. O is for open and C is for closed. 2 stands for either open or closed. ⎣⎡■C2C2222■⎦⎤ giving 25 possible versions. ⎣⎡■OCCC222■⎦⎤ and its symmetrical equivalent giving 2⋅23. ⎣⎡■OCOC2C2■⎦⎤ which gives 22.
That is the end of the reusable ones. (1)
⎣⎡■OOCCC22■⎦⎤ giving 22. and its horizontal equivalent.
⎣⎡■OOOCCC2■⎦⎤ giving 21. Note the symmetrical equivalent would close the 'door' to the maze and hence is not possible. and finally ⎣⎡■OOOOCOC■⎦⎤ giving just 1 (20 ).
Edit: The total is 59.
There is also an almost nice pattern if you 'overlay' the 2's. However I don't think this is useful in any way.
⎣⎡■02302242325■⎦⎤
(1) Note we cannot reuse the following ones as the 'Go-around path' exists: ⎣⎢⎢⎡■∣∣↼CCC→→■⎦⎥⎥⎤
Log in to reply
It seems like you missed a few. I like ordering by how many squares you can reach from the entrance (not counting the entrance itself) to get a complete list.
Exactly 0 squares: ⎣⎡■C2C2222■⎦⎤=32
Exactly 1 square: ⎣⎡■C2OC2C2■⎦⎤ and symmetry =16
Exactly 2 squares (you missed the last one): ⎣⎡■OCOC2C2■⎦⎤=4⎣⎡■OOCCC22■⎦⎤ and symmetry =8⎣⎡■OCCOC2C■⎦⎤ and symmetry =4
Exactly 3 squares (you missed the second and third): ⎣⎡■OOOCCC2■⎦⎤ and symmetry =4⎣⎡■OOCOC2C■⎦⎤ and symmetry =4⎣⎡■OCOOCCC■⎦⎤=1
Exactly 4 squares (you missed both): ⎣⎡■OCOOCOC■⎦⎤ and symmetry =2⎣⎡■OOOCCOC■⎦⎤=1
Exactly 5 squares: ⎣⎡■OOOOCOC■⎦⎤=1
For a total of 77.
After pulling out the computer programming, I managed to calculate how many mazes there are of every size from 2x2 to 5x5.
2 x 23 x 34 x 45 x 53513.8281.225.194
Here's a link to the Github with the current Python code. Hopefully it's commented well enough to be understandable.
Log in to reply
It should just be noted as these numbers are getting big that the largest integer python can handle is 231−1. So to continue this method we will have to use something like GMP. (That is only for C++ I think but it's the only one I can recall)
It would be helpful if you uploaded the code to github so we can make contributions to your code. (Also, the is an extra * on line 72 which breaks the code at the moment.) -> Only on python 2.7
Log in to reply
I've never used Github before, but I suppose it could be helpful. The link is now pointing to the Github repository I set up.
About the *, on my computer, line 72 works correctly. Line 72 is supposed to print a list. Without the *, the line prints the list including the [' '] around every element. With the *, the line doesn't print those. which makes the output look a lot cleaner.
I tested the code in the coding environment on Brilliant, and there it works like I want. I use Python 3.6. Are you maybe using an earlier version?
Log in to reply
While working around with some computer code, I discovered a pattern for 2xn mazes. The first 10 values are:
n12345678910Amount of mazes138204911928869616814059
When throwing this sequence in The On-line Encyclopedia of Integer Sequences, it returned this page.
The page includes the following recursive formula, which is the same formula as the partial sum for pell numbers:
a(n)=2∗a(n−1)+a(n−2)+1 with n>1,a(0)=1,a(1)=3
It also includes the following direct formula:
4(1−2)n+2+(1+2)n+2−2
Here's a proof for why the recursive formula applies:
For every column there are four types:
Type 1: [O■] type 2: [OO] type 3: [■O] type 4: [■■]
A black square means the square is closed; an O means the square is open. To create a valid maze of size 2xn, the following conditions must be met:
If the last point gets put aside temporarily, the following three recursive formulas follow:
M(n,1)=M(n−1,1)+M(n−1,2)M(n,2)=M(n−1,1)+M(n−1,2)+M(n−1,3)M(n,3)=M(n−1,2)+M(n−1,3)
M(n,t) = the amount of partial valid mazes with n rows and that ends with a column of type t
n = the amount of columns
And these are the base cases for when there's only one column:
M(1,1)=1M(1,2)=1M(1,3)=0
Here are the values of M(n, t) for n = 1 to 10:
nType 1Type 2Type 3111022213453491285212920650704971201691198289408288969798569610168223781681
Note that M(n, 1) is always 1 more than M(n, 3). To bring the last point back in, the calculation of the amount of complete valid mazes of 2xn is:
T(n)=M(n,2)+M(n,3)
To complete the proof, it's required to proof T(n)=2∗T(n−1)+T(n−2)+1
T(n)=M(n,2)+M(n,3)Substituting using M(n, 2) = M(n-1, 1) + M(n-1, 2) + M(n-1, 3) and M(n, 3) = M(n-1, 2) + M(n-1, 3)T(n)=M(n−1,1)+M(n−1,2)+M(n−1,3)+M(n−1,2)+M(n−1,3)Combining similar terms and factoringT(n)=M(n−1,1)+2(M(n−1,2)+M(n−1,3))Substituting using T(n) = M(n, 2) + M(n, 3)T(n)=M(n−1,1)+2T(n−1)Substituting using M(n, 1) = M(n-1, 1) + M(n-1, 2)T(n)=M(n−2,1)+M(n−2,2)+2T(n−1)Substitution using M(n, 1) = M(n, 3) + 1T(n)=M(n−2,3)+M(n−2,2)+2T(n−1)+1Substitution using T(n) = M(n, 2) + M(n, 3)T(n)=T(n−2)+2T(n−1)+1□
These results lead to some follow up questions. Why is the amount of 2xn mazes related to pell numbers? Pell numbers are strongly related to 2. Does that mean the amount of 3xn mazes is related to 3?
Log in to reply
Wow! This is very nice! (Also, check, you have a few LaTeX errors.)
Log in to reply
There, all the LaTeX is working like I want. I wish the comment section had a preview button to make it possible to spot errors in the LaTeX before posting.
Log in to reply
After being inspired by the recursive approach from an earlier comment of mine, I decided to use the same strategy on 3xn and 4xn grids.
First of all, notice that if a 3xn grid is solvable by only moving up, down, right, and left, it's also solvable by only moving up, down, and right. So movement to the left can get ignored (this becomes relevant later on).
Now, look at all the ways a column could look like:
Type 0: ⎣⎡■■■⎦⎤ type 1: ⎣⎡O■■⎦⎤ type 2: ⎣⎡■O■⎦⎤ type 3: ⎣⎡OO■⎦⎤ type 4: ⎣⎡■■O⎦⎤ type 5: ⎣⎡O■O⎦⎤ type 6: ⎣⎡■OO⎦⎤ type 7: ⎣⎡OOO⎦⎤ type 8: ⎣⎡O■X⎦⎤ type 9: ⎣⎡X■O⎦⎤
Where O means a square that's open and reachable without moving left, ■ means a closed square, and X means a square that's open but impossible to reach by only moving to the right, up, and down.
The types 0 through 7 are the 8 ways to have a square open or closed. But type 5 is different. It has two open squares that aren't connected. Because of that it's possible both can get reached, just the one at the top can get reached, or just the one at the bottom. To figure out if a maze is solvable, it's required to make a distinction between these three. Because of that type 8 and 9 got added. For type 5, both the top and the bottom can get reached. For type 8 it's only the top, and for type 9 only the bottom.
When constructing an 3xn maze column for column, it's required to make sure that with every column that gets placed, it's always possible to reach the rightmost wall of the sub-maze. For example, after a column of type 1, it's not allowed to place a column of type 2, because the two columns don't have a single connection. After a column of type 6, it's allowed to place a column of type 9, because there's still a path at the bottom. However, after type 6, it's not allowed to place one of type 5, because that one requires it's also required to have a free path at the top.
Using that logic, it's possible to create the following table. The column on the left is the type, and the column on the right is filled with all of the columns that are allowed to come before a column of the specific type.
Type0123456789Allowed preceding typesNone1,3,5,7,82,3,6,71,2,3,5,6,7,84,5,6,7,95,72,3,4,5,6,7,91,2,3,4,5,6,7,8,91,3,84,6,9
The first column in the grid has to be one where the top is accessible. Those are the types 1, 3, 7, and 8.
The last column in the grid has to be one where the bottom is accessible. Those are the types 4, 5, 6, 7, and 9.
Combining the previous table, together with the valid first columns, it's required to create the following table with the amount of 3xn grids that have at least one path to the right of the grid. Adding the numbers of the types that are allowed to be the last column leads to the number of complete 3xn mazes.
nType 1Type 2Type 3Type 4Type 5Type 6Type 7Type 8Type 9Complete mazes1101000110124241124308316122085142111351473679951268311047252955355359505295136461581219159163261796190626161632717249630701079915883079278100881368088303787133521622754915043472398484635334771903472392001471007857762844927225251261
The computer code that was posted earlier has also been updated to work with this efficient calculation.
Log in to reply
Were you able to find a recursive and/or direct formula for 3 x n mazes (like you found for 2 x n mazes)?
Log in to reply
I wasn't able to find a direct formula, and also no recursive formula that only uses the amount of mazes. The best I was able to find was a recursive formula for each of the 9 types, and a way of combining those to find the amount of mazes.
Some more statistics from an experiment with a board size 50 x 50:- A sample of 1000 million [ = one billion ] configurations was generated at random. In each trial, each square had exactly half a chance of being open or closed, [as computed the java Random class] except for the top left and bottom right squares that were always open. In this sample, 534 configurations had a path from top left to bottom right, [moving up, down or side ways, but not diagonally] along open squares. Thus configurations with paths are very rare. Amongst all configurations in the sample, the mean number of open squares in the island of open squares including the top left square and connected by moves as described was 11.781. Let the top left hand square have (x,y) coordinates (0, 0), so that its neighbor to the right has coordinates (1,0) and its neighbor below has coordinates (0,1). For each of these islands define "the furthest square reached" to be the square in the island for which the sum, S, of its x and y coordinates is the greatest. Then for each island, define S to be "the furthest distance reached". In the sample the mean of "the furthest distance reached" over all islands was 4.6768. Again I find this number surprisingly small.
An obvious approach to this problem would be to map out each unique path and configure the squares 'open and closed'. Trying to figure out which ones are the same is the only challenge then.
We also have the fact that each open square needs at least two open neighbors for a path to exist.
I'm guessing the paths are unique implies no double backing? i.e. a path A2 A3 A4 A3
A simpler version of the problem would be to only allow moving down and to the right. After thinking for some time about that simpler problem, I was able to figure out how to make a recursive algorithm that puts a range on the possible amount of solutions.
Let M(R,C) be a function that takes in the amount of rows (R) and the amount of columns (C) the grid has, and returns how many possible mazes there are. To get to the bottom-right, there has to be a path to the square one to the left of the exit, or one to the square above the exit.
It's quite easy to show M(1,C) = 1 for all C. The only way a path from the entrance to the exit can exist when there's only one row is when all the squares are open. Similarly, M(R,1) = 1 for all R.
When R and C are both at least 2, things get harder. There are three things that could happen:
The square to the left of the exit is open AND the square above the exit is closed. In this case the amount of paths to the exit is M(R,C−1)∗2R−2. The 2R−2 is to account for the fact the rightmost column has R-2 squares that may either be open or closed freely without creating a new path, or destroying an old one.
The square to the left of the exit is closed AND the square above the exit is open, the amount of paths to the exit is M(R−1,C)∗2C−2.
Both the square to the left of the exit and the square above the exit are open. This scenario is the hardest, because it's very easy to start double counting. This is a number that's at least MAX(M(R,C−1)∗2C−2,M(R−1,C)∗2R−2) and at most M(R,C−1)∗2C−2+M(R−1,C)∗2R−2.
To elaborate how the bounds got determined, let's first look at the upper boundary. That one looks at all the paths to the exit and doesn't bother taking out the double counting. The lower boundary only looks at all the paths that come in from one direction and assumes the other direction is open even though it never gets used in a path.
Using the above math, the following table can get generated. But again, it's assumed it's only allowed to move down and to the right.
R C1234511111121[3,4][8,12][20,32][48,80]31[8,12][48,96][272,640][1.472,3.840]41[20,32][272,640][3.264,10.240][37.888,143.360]51[48,80][1.472,3.840][37.888,143.360][909.312,4.587.520]
Log in to reply
If only movements downwards and to the right are allowed, then M(R,C)=(C−1R+C−2)
Despite this however, your recursive approach seems promising.
Log in to reply
What you describe is how many paths there are from the top-left to the bottom-right when all the squares are open. What I was trying to describe is how many unique mazes there are where at least one path exists.
To elaborate on the difference, let's look at the 2x2 grid. (C−1R+C−2)=(12)=2
To show there are more than 2 possible 2x2 mazes, here are three possible 2x2 mazes:
[OOOO][OOXO][OXOO]
An O represents an open square and an X represents a closed square. I hope this cleared up some confusion.
Log in to reply
@Jason Dyer, it looks like the question has already been asked before. The OEIS has this page about n X n grids. It also has multiple cross references to pages about grids of size 3 X n, 4 X n, etc.
Log in to reply
That's an obscure way to write it! Doesn't look like it has ever shown up in an article, either. We should definitely work on trying to get more general results then (like the 2xn you found).
I expect the combinatorics are too messy to get an exact or satisfying answer, but perhaps we could get some upper and lower asymptotic bounds on the number of configurations with/without a path? In particular whether the proportion of the 2^(n^2-2) configurations tends to 0 / 1 / (constant p in between).
Since my comment yesterday, I have been doing a bit of experimenting with random configurations, of size n x n ~ 30x30. It is clear that as n tends to infinity the proportion of configurations with a path top left to bottom right (TL - BR) tends to zero. This can be seen by noting that t the number of solid black paths bottom left to top right is the same as number of solid white paths (TL-BR) and each blocks a path we are looking for. In fact the number of blocking paths is much more any path from a square on left or bottom side to any square on top or right hand side will block. Furthermore blocking paths which are weakly connected -- that is allow diagonal connections instead of connections along a side will also block a path we are looking for. The conclusion is that paths [we are looking for] are surprisingly rare.
If you take a large number of random configurations of open/closed squares for any board of moderate size (anything over n = 20) will do, then the mean size of the island consisting of connected open squares which includes the top left hand square is between 11 and 12. By the time n = 50, mean size of this island has settled down to its asymptotic value of 11.78.
A few more thoughts, without trying to give exact results: 1. Let the board be n cells by n cells and consider a large sample of configurations in which each cell has half a chance of being open = white or closed = black, except for the north-west cell. In the original problem the south-east square is also open, and in this case define P[n] to be the probablity of there being a path between these two squares moving only horizontally or vertically between adjacent open squares. If the south-east square only has half a chance of being open, define R[n] to be the probability of a path between the two squares and note that P[n] = 2 R[n]. 2. Now let the board be semi-infinite: that is let the board have north-west square with coordinates (1,1) [not (0,0) as in my earlier posts] and extend infinitely far to the south and east. Again consider a large sample of configurations in which each cell has half a chance of being open or closed apart from the north-west square which is always open. Let Q[n] be the probability of a path from this square to the square (n,n). By experiment, we find that Q[n] is roughly about twice R[n]. In other words, there are many paths which go beyond the cells with x or y coordinates equal to n, but double back to reach the square (n,n). {It remains true that Q[n] and R[n] are small in absolute size.} 3. On this semi-infinite board, define D[n] to be the probability of a path from the north-west square reaching any cell (x,y) on the cross diagonal, where x+y = n + 1. 4. By experiment, on values of n up to about 50, we find R[n+1] / R[n] -> a constant around 0.7 to 0.8, with similar results for P, Q and D, where the constant has a similar but different value. Comments:- A) (4) implies that the number of paths tends to K.( 2^(n^2 - 2 ) ).(k^n), where K and k are constants. B) (2) suggests that a more natural problem is to find the probability on semi-infinite board or one which is infinite in all directions. C) I have tried normalising the probabilities R[n] by the total on the the diagonal, D[n], and comparing it with the values in Pascal's triangle, without getting any decisive result. However, I think there must be something in this approach, especially as Pascal's triangle is related to the number of paths from the north-west square to the another given square. It would be natural to extend the notion of Pascal's triangle by including paths which turn back on themselves.