original maze-creation algorithm ideas

Status
Not open for further replies.
My C++ professor recently gave us an assignment to read in a text file of 0s and 1s into a 2d array as a rectangular maze, and then traverse the maze by following a wall.

As an extra he challenged us to come up with a random maze generator. I worked for hours on this but could not come up with an algorithm that gives suitable "maze-ness" (solid clusters of walls, and gaps, connectivity and dead ends) He said it doesn't matter if it looks like a maze or not, but I was still intrigued..

good maze bad mazes
11111111 11111111 11111111
10110000 10101000 10001000
10100011 00101101 10000001
00001001 11010101 00000101
11111111 11111111 11111111

I tried all sorts of tricks like XORing neighboring values to create
walls and hallways, to no avail. My current algorithm simply creates a frame of 1s, element is set to (rand() % 3)%2 + '0' (seems to yield best results by experimentation) and then an entrance and exit pathway is cleared on either side. It generates rather pathetic mazes!

Some possible ideas which I haven't tried yet:
- a random first pass, and then a second pass which randomizes again with probability based on the surrounding squares.
- fill entirely with 1s and then create some sort of "boring" algorithm which works like a blind search function which wanders around twisting and turning, until an exit is found.

I know there are 'real' algorithms out there by professors and PHDs but I'm interested in your original ideas.. bonuses for elegance and simplicity over mathematical rigor, and incorporating seemingly unrelated ideas.. anyone?
 

bugbum1

Ars Scholae Palatinae
893
why create new ideas when good algorithms exist to solve your problem?

1) Think of each element in the 2D matrix as a position having a total number of "connections" (legal moves to other elements).

2) Generate your random walk constrained by this rule - never enter a position whose connections to connections are less than a cutoff value.

this is known as a greedy algorithm and is used to solve knight's tour problems (generate a circuit on the chess board moving a knight to each square only once). in this case, the path is the tour, and the greedy algorithm prevents the path from crossing itself.

an implementation of this exists in pohl's A Book on C (if you have access to a library). if not a few links the ultimate Knight's Tour page of Links
 

bugbum1

Ars Scholae Palatinae
893
the random walk is as random as a finite state automata can generate - the fact that it is bounded by an algorithm makes it less random?

in light of the fact that your approach has not yeilded results, i'd concentrate on getting the problem solved before being creative about the solution. can you tell i've been programming too long? seriously, often the best implementations are the refinement of known techniques. creativity comes in many forms, so in your terms, there isn't just one way to be creative, or to implement a 'known solution'. sorry this solution doesn't fit the bill - i'd examine your criteria ;)
 

Statik

Ars Scholae Palatinae
1,451
I'd second the random walk routine with the following additions/changes (PS, this was also MY first year rpoject)

Have your algorythm (sp? Man I forget how to spell) know the dimensions of the grid.

Start a random walk routine in which you randomize the length of the walk (say 1 - 3) and the direction of the walk, exempting the previous direction (1 for up, 2 for right, 3 for down, 4 for left)

Input each move in the walk into an numbered array.

When you reach a boundry, step back through the array a random amount (between 1 and the number of elements in the array), and begin again.

Make sure to erase all the old elements of the array after the point you are beginning again.

Stop creating the maze when you reach the goal, and/or the amount of pathway to walls is 1/6.

Statik
 

Tegid

Ars Legatus Legionis
12,810
Subscriptor
Hmmmm this is an interesting problem. I have a couple of ideas.

Firstly, don't limit your creation steps to the final 0/1 paradigm. Even if you have to finish with a list of 0's and 1's doesn't mean your steps in between have to. Use other numbers to represent important concepts (like + intersections and such).

Second, does the ENTIRE maze have to be random? What if you predefined the solution path and then just generated the dead ends?

Third, a maze can be thought of as a big trinary tree. You have the node you came from, and three possible destination nodes. If you created a random list of numbers that sorted along the tree in some way, you could have predefined start and stop nodes (root and some number) and then just map the tree to a 2d grid. For instance...map it like a normal binary tree except that instead of ignoring values that are the same, you create a new child node that is "down" (instead of "left" or "right") and which then takes any more traversals that originally went through it's parent. You may have to have a step to flesh things out, but this could definitely work. (I'm thinking this up as I write and getting intrigued) This approach views a maze not as a collection of random hallways, but rather as a collection of paths which end at dead ends, and one path which ends at your solution. Maybe by suitably restricting the set of random numbers you generate, you can come up with differing amounts of straight long paths and turns.

I'm sure there are finishing steps to that process that need working out, but that's my most original idea, straight off the top of my head.
 

BitPoet

Ars Legatus Legionis
21,805
Moderator
I was thinking along the lines of a recursive algorithm, continually dividing the area of the maze into 4 parts.

Each section would need the 'borders' of the area defined, each block of the border would have one of a few different values:
1: it's a wall
2: it's an opening
3: an entrance for the path
4: an exit for the path.

Each level of the algorithm divides up it's remaining space into 4 pieces, generating the values along the 'cross'.

Keeping in mind some rules for the 'entrance' and 'exit' parts.

As long as the opening-to-wall ratio is pretty even, the maze ought to come out ok. The a problem might be generating other paths, rather than having just noise. Using the method used above for walls/openings to 'want' to be near something like it would probably be a good thing
 
Status
Not open for further replies.