How do you find the start and end of a maze generated with recursive backtracking?
It seems like it would be hard to figure it out, as the maze never ends
. Would it be the point where you first start backtracking? The start point could be where you start, but sometimes there is a better spot.

- 481
- 6
- 18
-
You could define a maximum number of steps and increase this number after each combination was found. – Philipp Braun Jan 30 '17 at 19:46
1 Answers
When you generate a maze with recursive backtracking like this:
http://weblog.jamisbuck.org/2010/12/27/maze-generation-recursive-backtracking
then all the cells are connected, and there's exactly one way to travel between any two cells without retracing your steps.
You can pick any start and end points you like!
Note that a graph that is connected like the maze cells are, i.e., so that there is exactly one path from any vertex to any other vertex, is an undirected tree. If you want to find the two points that are furthest apart, so that you can use them as the start and end points, then you need to find the diameter of this tree.
There lots of ways to do that, but the simplest is:
1) Pick a random vertex to start at, and use BFS to find the/a vertex that is farthest from it. That will be your start point.
2) Use BFS to find the/a vertex that is farthest from the start vertex. That is your end point.
The start and end points will be as far apart as possible.
The answer to this question explains why that always works: Proof of correctness: Algorithm for diameter of a tree in graph theory
Note that the points that are furthest apart aren't necessarily on the edges. I find that picking random points on the edges works fine when that's what you need: https://mtimmerm.github.io/webStuff/maze.html

- 1
- 1

- 53,709
- 3
- 46
- 87
-
I want to find the spots where the distance is greatest, therefore making it hardest. – lol Jan 31 '17 at 01:25