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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.