In one of my hippy college CS classes, we had to write a maze solver. I did a cute trick to make it easier to handle backtracking.
The trck did not lie in the program, but in the maze encoding. Assign a prime number to each direction that you might want a maze walker to go. Then each square gets the product of the directions that are available.
Backtracking is easy in this case, you just start at your highest (or lowest) possible factor, and work your way down the list. Plus you can define an infinite number of directions for your maze to turn ;)
If up is 2, right is 3, down 5 and left 7, your maze above would be:
Initial reduction: 15, 21,105, 21,105, 21, 35 2, 0, 10, 0, 10, 0, 10 0, 0, 30, 21, 70, 0, 10 3,105, 14, 0, 30, 21, 14 0, 10, 0, 0, 0, 10, 0 0, 30, 21, 7, 0, 30, 35 0, 0, 0, 0, 0, 0, 2 Remove any rows w/ only verticals: 15, 21,105, 21,105, 21, 35 0, 0, 30, 21, 70, 0, 10 3,105, 14, 0, 30, 21, 14 0, 30, 21, 7, 0, 30, 35 Remove any columns w/ only horizontals: 15, 21,105,105, 21, 35 0, 0, 30, 70, 0, 10 3,105, 14, 30, 21, 14 0, 30, 21, 0, 30, 35
I think I spent to much time on Godel's proof!
TGI says moo
In reply to Re: shortest-path maze solver
by TGI
in thread shortest-path maze solver
by premchai21
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |