Breadth first maze solution
July 21st, 2008
I know, I know – back-to-back entries, the sky must be falling.
I felt so guilty about posting that code yesterday that I was compelled to get this second version finished off tonight and optimized. And good news - I’m clocking it at 7 seconds! The big secret (ok, Bionic-Badger apparently already knew it in his comment a month ago) is to remove all the data constructs and just jam through the bitmap directly. So that’s what I did.
I had a brief notion that recursion was the way to go (I loves me some recursion!), the code was benchmarking extremely fast since there isn’t any storage, every child node just gets scoped into its own closure, however around fifteen thousand nested function calls is where Flash draws the line and throws in the towel (or stack overflow error, as the case may be). I’m still pretty sure I could get this algorithm down into the 2-3 second range if the recursion had worked...
Anyhow, my strategy was to color each explored pixel with an incremental number, so the first pixel becomes colored 1, the second 2, etc. Since we’re in hex color land, this gives the weird side effect of black to blue gradients that shift up into the green space as the color channels overflow. It’s bizarre to look at, but kind of cool to see where the breadth-first approach explores different areas. After that mapping has taken place, it’s a simple matter of tracing the path back from any given pixel by repeatedly finding its lowest-valued neighbor.
One final note; I’ve been noticing a strange discrepancy in the memory traces. About half the time I’m seeing 30-35mb and the rest of the time 2-3. I know this code is spinning off a lot of throw-away variables so I’m inclined to believe the higher numbers and speculate that the lower numbers are results of the Garbage Collector running at opportune times.
Maze pathfinder 2
source [748k]
[sad excuse for not posting goes here]
July 20th, 2008
A few weeks ago now, I got a comment on my last post asking for the source. The real (dirty little) reason I took so long on this, is that I was trying to find some time to rewrite this stuff and post a much better version alongside, but it just didn't happen. I’d still like to get a new version of this thing put together – I’m confident I can beat that 11 second benchmark – but in the meantime, anyone reading can take a chuckle at my schlocky earlier version. And for the curious, here’s a link back to what I originally developed that code for - which is a roundabout apology/defense of my (very) liberal memory hogging.
Maze pathfinder [1mb]
