[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Maze



Jerome Quelin wrote:
> 
> How would
> *you* have done if you were to create a maze ? (Do you know
> an algorithm that create a maze ?)

Here's one.

You start with all the outer wall segments filled in.  Say the
entrance is at the top and the exit is at the bottom; then the
solid wall on the left is colored "blue" and the on the right
is colored "red".  Until you can't do it any more, you select
a wall segment at random from the interior.  Don't consider
ones which are already colored, which means it's already been
made "solid".  Now, if the selected segment touches another
which is colored, it itself becomes that color.  But if it
touches color at both ends, then you must leave it uncolored.
(If it touches the same color at both ends, then to make it
solid would close off some portion of the interior from
reachability, and if it touches blue at one end and red at
the other, then it makes the maze unsolvable.)
This algorithm is very easy to optimize, too.

-- 
John Porter

What would Gabrielle do?


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe