1

How to efficiently exit from a maze where you know the initial position of the player (1,1), the exit (49,49)?

You don't know the maze configuration but you know where your player is, and which direction are opened from his position.

Another point, the player can move RIGHT/DOWN/LEFT/UP, and the exit is at the bottom right.

I tried to mainly follow RIGHT/DOWN and avoid dead ends, but I think we can do better!

Yuval Filmus
  • 280,205
  • 27
  • 317
  • 514
KKc
  • 111
  • 1

1 Answers1

1

The fact that the exit is in the bottom right and has coordinates $(49, 49)$, while the player has coordinates $(1,1)$ makes me think that the maze has a fixed size of $49 \times 49$. If that's the case the exit can be found in constant time.

In fact, there is a fixed strategy (i.e., a sequence of moves) that uses a constant number of moves and exits the maze, regardless of the maze layout!

You can construct this strategy as follows: enumerate all possible maze layouts $L_1, L_2, \dots$ (a crude upper bound on the number of mazes is $2^{49^2}$).

You can construct the sequence $S$ of moves iteratively. Initially $S$ is the empty sequence. Then, for each $L_i$ you can extend $S$ as follows:

  • Assume that the maze layout is $L_i$, and compute the position $p$ in which the player would end up by following the steps in $S$ from position $(1,1)$ in $L_i$.
  • Find a sequence $S_i$ of at most $49^2$ moves that make the player walk from $p$ to $(49,49)$ in $L_i$. This sequence always exists and can be found, e.g., with a breadth first search from $p$ in the graph induced by $L_i$.
  • Append $S_i$ to $S$.

The resulting sequence $S$ has length at most $2^{49^2} \cdot 49^2 < 2^{2413} = O(1)$.

Notice that the above algorithm is only needed to construct $S$ once and for all. An algorithm that solves your original problem can just output the hardcoded strategy $S$, without even looking at its input.

Steven
  • 29,724
  • 2
  • 29
  • 49