I got asked the other day about "Outlining a general algorithm for solving a maze with n balls, where the goal is to get all the balls to a given position in the maze (the maze has no exit)". The only rules are that the algorithm must be effective (better than randomly moving the balls) and that all the commands issued will affect all the balls, so that is one ball is moved north, so will all the others if they are not blocked.
To do this I made some assumptions, namely that
- The maze is standard/perfect
- The balls cannot block each other
- The balls can get to the position asked
- A command will let the balls roll until the hit a wall
- A command can only be N/S/E/W
- The maze is randomly constructed and the balls randomly distributed each time it is reset
- All of the maze can be observed at once by the maze-operator
And, to make my algorithm work
- The balls are not identical (e.g. the have a number or something on them)
- The maze-operator has a photographic memory
Given this, I thought the best idea would be to
- Randomly (or in a smart way) move the balls until two balls get to the target position
- Save the path from their starting position to their end position.
- Identify those balls and where they came from, and for each of them, do 1.
The "break" in this recursive algorithm would be when all the balls have a way to reach the given target (O(log(n)) recursions I think?)
Does this work? Does anyone else have a better algorithm for doing this?
I had another idea involving moving all the balls to the same random position and then moving them all as one ball, but that seemed like a worse algorithm.
Another idea would be to generate a graph (graph theory that is) where all the stable points for a ball would be a node, and the move would be an edge, but I can't see how that doesn't need a lot of brute force to be done.