18

Given a 2-dimensional maze where you can give 4 commands "move up/down/right/left". Knowing the maze but not where the person is, how to find the minimum sequence of commands that guarantees exiting the maze? I'm looking for a single sequence of commands that will work no matter where in the maze you start from.

Assume that if our partner is given the "move right" command when there's a wall on the right, he will simply stay where he is.

In other words, we're given a maze, and we must choose a sequence of commands. Then, our partner will be placed somewhere in the maze and will follow the sequence of commands we've chosen in advance. We want this sequence to ensure our partner will escape, no matter where our partner was initially placed. Note that the allowable commands do not have any conditional statements, so they cannot follow a different sequence depending on your partner is.

Is there a polynomial-time algorithm to construct such a sequence, given a description of the maze?

Yuval Filmus mentions this is a special case of a synchronizing word problem, and might be related to universal traversal sequences. I also found a paper that seems relevant:

The Simultaneous Maze Solving Problem. Stefan Funke, André Nusser, Sabine Storandt. AAAI 2017.

Unfortunately for general graphs this appears to be a unsolved problem, but I'm wondering if there might be a good algorithm for this specific case. I came up with a candidate approach: Label every position with the number of minimum steps it requires to exit, and keep track of every agent in the maze. It might be possible to do a A* search this way.

D.W.
  • 167,959
  • 22
  • 232
  • 500
seilgu
  • 281
  • 1
  • 3

3 Answers3

0

One optimal solution for this problem is A* with iterative deepening (IDA*) as described in the accepted answer to my puzzle. I am not sure whether IDA* is a polynomial-time algorithm in general, but the following sheds some light.

0

Let’s say there are n positions in the maze, there are not all walls around you so leaving is possible, then for each position you can find the shortest path to the outside, and that path has length <= n. Getting out in n^2 steps is easy:

For each possible position find the shortest path to the outside and its length. You know you are in one of n positions. Find one with a shortest path to the outside and turn that path into instructions.

There are k >= 1 starting positions where you are now outside (for example all positions left of an exit). So either you are on the outside, or in one of n-k positions, and you can determine which ones. Actually, the number of possible positions is often less than n-k, because the walls can turn multiple start positions into the final end position. If you tried the path “one step to the left”, then being exactly to the right of a wall and one step away will move you to the same place at the right of that wall.

If you are not on the outside, you repeat the same method again until you reach the outside. I’m not sure if choosing the shortest paths first is optimal; if there are 200 positions in the labyrinth then a path of length 150 would have a reasonable chance to get outside, plus many possibilities to reduce the number of points where you can end up.

This assumes that the only feedback you get is when you are outside the wall.

Iterative deepening might take exponential time. Let’s say the longest shortest path has length 100. Trying fewer than 100 instructions won’t work. Try 100 instructions: First you need an instruction that leads to a point with distance <= 99, wherever you are. Calculate all points that this instruction leads you, then find an instruction leading only to points with distance <= 98, and so on. If this doesn’t work you try 10- instructions and so on.

In this case if you try to find a sequence of 150 instructions, you would have a free choice for the first 50, so you need to remove duplicates somehow.

gnasher729
  • 32,238
  • 36
  • 56
-5

An algorithm that always works is: Put the left hand on the wall, and continue that way to the exit. Can't guarantee shortest path (to do so, you need to know the maze, at least partially, and be able to look forward. Check out the $A^*$ (A-star) algorithm, it was originally designed for just such tasks).

vonbrand
  • 14,204
  • 3
  • 42
  • 52