14

I went back in an old Flash game and was intrigued by this problem.

Rules

  1. There is an $n\times n$ grid and a mouse entering on the entrance cell and exiting on the exit cell.
  2. The entrance cell and the exit cell is found outside on the grid, on the top and bottom side respectively. You can move them around.
  3. You can build any number of walls in the grid, as long as it does not block the exit cell.
  4. When the walls are built, the mouse will be released.

    The mouse moves as follows:

    • The mouse moves one step towards one of the four cardinal directions: south, west, east, north.
    • The mouse moves to the square it has visited the least.
    • When there is a tie, the mouse prefers the order: down, right, left, up.

Here is an example animation on a $4\times 4$ grid: (Note that this is not the optimal solution, and you can use other $n\times n$ sized grids as well)
Mouse Maze problem

Questions:

  1. What is the maximum number of steps the mouse can have on a $n\times n$ grid?
  2. What is the maximum number of visits on the same cell can you have on a $n\times n$ grid?
melfnt
  • 5,132
  • 2
  • 13
  • 58
u-ndefined
  • 5,181
  • 1
  • 12
  • 49
  • Can the mouse reenter the entrance cell? (also, is the answer known?) – the default. May 29 '20 at 12:10
  • I have a gut for the second question, but I couldn't prove it.. – athin May 29 '20 at 12:31
  • 2
    @mypronounismonicareinstate The mouse cannot reenter the entrance cell. The answer isn't known too, it looks like the problem needs brute-forcing / computer to find the max. – u-ndefined May 29 '20 at 12:37
  • @u_ndefined While it can be brute-forced for a small value of $n$, I'm not sure how that will lead to the answer (unless if an OEIS argument is possible). – the default. May 29 '20 at 12:39
  • 2
    This game looks familiar! You may be interested in this related math paper: https://drops.dagstuhl.de/opus/volltexte/2016/5874/ – Stomf May 29 '20 at 17:58
  • For a sufficiently large n, I think I have a wall layout that can be used to build a binary counter (I just need some more work to formalize it). Thus, the maximum number of step should grow exponentially. – SE - stop firing the good guys May 30 '20 at 15:51

5 Answers5

9

While writing some code to attempt to beat the high scores in the Mouse Maze 2 game (which is the game described, but on a 9x9 grid), I was testing it by brute forcing 4x4 mazes and comparing the results with the current brute force answer.

Apparently, this time brute force reveals the actual solutions for 4x4:

Most steps: 86 (up to off by one errors when counting steps)

86-step illustration

Max visits for a single cell: 16

16-visit illustration

I have also brute forced all 5x5 mazes (6x6 will take a few millions as much time):

Most steps: 283

enter image description here

Most visits for a single cell: 59

59-visit illustration

I tried to optimize for good 6x6 mazes (of course, the results probably won't be optimal).

Most steps: 1368

1368-step illustration

Most visits on one cell: 201. I suspect this is optimal, because it seems like almost all threads produce this solution or a very similar one with the same score.

201-visit illustration

The approximation algorithm is a local greedy search. It makes up to 5 random changes (the number of changes is chosen randomly), and if no improvement is seen within 300000 steps, it accepts this as the solution and starts a gain. The program is multi-threaded and each thread runs for 1 billion steps.

My best score for 9x9 mazes is 74283 steps with 5784 visits on a single cell. This seems to be better than the current record.

enter image description here

the default.
  • 633
  • 5
  • 9
  • Out of curiosity, what language do you use for brute-forcing problems like this? – Ontonator Jun 07 '20 at 05:58
  • C++ with OpenMP, compiled with g++ -march=native -Ofast -flto -fopenmp -no-pie [warnings and stuff]. – the default. Jun 07 '20 at 06:07
  • For 6x6 and up, it might be interesting to use some kind of genetic algorithm to find solutions. Of course it will be impossible to prove whether found solutions are optional, but maybe it gets some nice results. – sarsaparilla Jun 07 '20 at 11:45
8

Apparently there was a bug, and my brute-forcer missed some solutions. The actual answers are in a different answer below. I have fixed the bug in my script, and can confirm that the answers provided there are correct (or at least match mine).

Original answer:

Brute force reveals the solutions for 4×4:

Most steps: 78

+---+   +---+---+
| 1   3   3   4 |
+   +   +---+   +
| 1 | 1 | 5   5 |
+   +---+   +---+
| 1 | 8   9   5 |
+   +   +---+---+
| 1 | 9  12   9 |
+   +---+---+---+

Note that although they sum to 77, there is also another step for stepping out of the exit at the bottom, which is not accounted for by the cell visit counts.

Max visits for a single cell: 14

+---+   +---+---+
| 1   3   6   3 |
+   +   +   +---+
| 1 | 1 | 6   6 |
+   +---+---+   +
| 1     | 6  14 |
+   +   +---+   +
| 1         | 6 |
+   +---+---+---+

These solutions are not necessarily the only ones with these counts, just the first ones that I generated with my script.

Ontonator
  • 231
  • 1
  • 3
  • Did you just stick with the entry/exit positions from the OP's example, or does this include varying those two elements as well? – Paul Sinclair May 29 '20 at 18:54
  • @PaulSinclair it accounts for different positions. It’s just that they work really well for boxing the mouse into the side of the screen without the exit. – Ontonator May 29 '20 at 18:56
  • 2
    @Ontonator I feel like this could be a competitive programming-type question. I wonder what they would be able to come up with. – rinspy May 29 '20 at 19:51
  • I think there is at least a partially mathematical approach here, too. I've been looking at these examples, and I suspect there is a way to calculate the visit-totals for each cell without having to follow the path and count. Such a method could evaluate particular mazes more quickly, and maybe give ways of identifying potential candidates without having to test every one. But I haven't fully worked it out yet. The basic idea is the "pressure" needed to push the path in the correct direction, based on how many wrong directions it has to go through first. – Paul Sinclair May 30 '20 at 00:06
  • Nice. Can you brute-force it for other $n\times n$ sized boards as well? – u-ndefined May 30 '20 at 09:57
  • @u_ndefined I could for $n < 4$, but I suspect that $n > 4$ will take too long. The time complexity to brute force all wall configurations is $\mathrm{O}!\left(2^{n^2}\right)$ assuming that all board sizes have the same average number of steps (which is unlikely, I suspect that it increases exponentially with $n$ as well, although I have no basis for that), so it will probably take too long. $n = 5$ is probably possible, but that would probably take multiple days, depending on the computer. – Ontonator May 30 '20 at 10:05
  • @PaulSinclair I see what you mean, there is a definite pattern. I'm not sure if you would be able to calculate an exact figure, but it definitely looks like you could approximate it, and then use that to design configurations with high numbers of steps or visits, or use it to narrow down a brute-force search. – Ontonator May 30 '20 at 10:09
  • Unfortunately, I haven't been able to spot how to "back-propagate" the pressures. I still suspect it is possible, but it is proving elusive. – Paul Sinclair May 30 '20 at 17:38
  • My brute forcer seems to be getting different (better) results. – the default. Jun 04 '20 at 16:39
5

Not sure this is the upper limit, but this layout gets 10 visits to a single cell:

+---+   +---+---+
|     1 | 2 |   |
+   +   +   +---+
| 1   3   8   3 |
+   +   +   +---+
| 1 | 1 | 4 |   |
+   +---+   +---+
| 1 | 4  10   4 |
+   +---+---+---+

OLD:

+---+   +---+---+
|     1 | 2 |   |
+   +   +   +---+
| 1   3   9   3 |
+   +   +   +---+
| 1 | 1 | 3 |   |
+   +---+---+   +
| 1             |
+   +---+---+---+
Ontonator
  • 231
  • 1
  • 3
4

As the comment here and other answers indicate, this answer is INCORRECT. I leave in case the idea behind bounding the number of exits from a square can be used with other arguments to construct a bound.

For the second question, I'm pretty sure the maximum is four. To see four is achievable, consider:

Maze

The mouse will visit the square in the second row, third column 4 times: the initial entry from left, returning after exploring the alcove below, returning after exploring the alcove at right, and then returning after exploring the alcove above.

The rules, namely the preference for least visited squares, ensure that once you exit a square in one direction, you will not return to it until there are no unvisited squares reachable in that direction. Since the game must terminate, for every reachable square there must be some direction you can leave it and reach the exit. Thus, you can only leave a reachable square at most four times. Since the game begins outside the maze, this means you can only enter a square at most four times.

You can use this to bound the maximum number of steps to $4n^2$, but that is doubtless far from the actual maximum.

Jeremy Dover
  • 27,440
  • 3
  • 66
  • 157
  • 3
    Correct me if I'm wrong, but doesn't it cross that square 6 times? It would do exactly what you have stated, but once it has explored all of the alcoves, there is a tie, and it would prefer to go down, then right, then finally left again, so it enters from the left, bottom, right, top, bottom and right, in that order. – Ontonator May 29 '20 at 15:11
4

Don't have an answer yet, but I think for the second question at least 5 is actually possible:

enter image description here

By having a visit count of 2 on the 2;2 cell, we can force the mouse to go in circles in the right sector, visiting column 3 row 2 cell 5 times before being able to head left towards the exit.

rinspy
  • 425
  • 2
  • 11
  • Ahh...this is correct and I retract my answer! I did not think of this case. – Jeremy Dover May 29 '20 at 15:17
  • The path in the diagram is actually slightly wrong, as the mouse does an extra loop in the top right corner because the other option already has 2 visits, although this doesn't change the maximum visit count of 5. – Ontonator May 29 '20 at 15:30
  • @Ontonator you are right, I miscounted the visits of the dead-end cells. – rinspy May 29 '20 at 15:33