How to generate mazes with fixed entry and exit points?
<p>I have read about the Depth-First search algorithm to create and solve mazes. However, I have not found anything on creating mazes with fixed entry and exit. On each maze the entry would always be at (0, 1) and the exit at the opposite side on both axis.</p>

<p>During the generation of the maze, every cell should be visited (to generate as many dead-ends as possible) but the the exit should always be at the same point.</p>

<p>The solution path to the resulting mazes would look like this:</p>

<p><a href="http://i.stack.imgur.com/nBnI1.png" rel="nofollow"><img src="http://i.stack.imgur.com/nBnI1.png" alt="enter image description here"></a></p>

<p>Or this:</p>

<p><a href="http://i.stack.imgur.com/1AeKX.png" rel="nofollow"><img src="http://i.stack.imgur.com/1AeKX.png" alt="enter image description here"></a></p>

<p>I've made grid-based mazes before using a breadth-first search, but a similar algorithm can be devised from a depth first one.  </p>

<p>First I'd create a <a href="https://en.wikipedia.org/wiki/Graph_theory" rel="nofollow">graph</a>, where each node in your coordinate graph above links to the node up, down, left, and right of it.  For example, node (1, 1) has an edge to (0, 1), (1, 0), (2, 1), and (1, 2).  Edge nodes will only have 3 edges, and corner ones will only have 2, since the neighbors in the appropriate directions don't exist.  While you are generating these edges, assign each one a random weight.  When I implemented it I found that a range of [0, 100) worked well, but you can tweak that.  Then finally you can do a Depth First Search from your desired start node to the desired end one, and just trace out the path as you go.  If you recurse down an edge that would connect you to a node you've already visited, don't draw the edge there.  That'll get you something that looks maze-like.</p>

<p>When I did it, I actually set up the graph in the same way, but instead of DFS I used <a href="https://en.wikipedia.org/wiki/Prim%27s_algorithm" rel="nofollow">Prim's Algorithm</a> to calculate the minimum spanning tree of that graph.  This gave me something that looked maze-like, touched every node at least once, and contained no cycles.  Then I could assign any point I wanted to be the start and end point, and the maze would contain no cycles, and exactly 1 shortest path between any 2 points.  I added tools on top of that for editing the maze, removing dead ends, rendering it in 3D, etc, but those are beyond the scope of your question.</p>

<p>If you want to see how I did it, <a href="https://github.com/CodyIsAwesome/Minotaur" rel="nofollow">check out my project</a> on GitHub.  The "Minotaur" folder contains the executable (a Frankenstein's monster of Python, C++, and C#), and the source is in there too.  The <a href="https://github.com/CodyIsAwesome/Minotaur/blob/master/Source/CPP/PrimMazeGenerator/PrimMazeGenerator/Graph.cpp" rel="nofollow">maze generation part is in this file</a>.   </p>

<p>I know you asked for this in Python but I'm too busy reverse engineer my C++ code right now, I hope you still find this answer helpful.</p>

<p><em><strong>Edit:</strong></em> I nearly forgot I made a fancy video showing it off, so if you want to see it in action but don't want to compile my source or don't trust my executable, <a href="https://codyachilders.com/portfolio/procedural-maze-generator-2015/" rel="nofollow">you can view the project on my portfolio</a>.</p>

python depth-first-search maze