Data structures created some pretty intense code and I was surprised that the C++ solution was cleaner in some ways.
And dirtier in others ;)

For instance, this:

sub step { my ($dir, $r, $c) = @_; if ($dir == N) {return (--$r, $c)} elsif ($dir == NE) {return (--$r, ++$c)} elsif ($dir == E) {return ($r, ++$c)} elsif ($dir == SE) {return (++$r, ++$c)} elsif ($dir == S) {return (++$r, $c)} elsif ($dir == SW) {return (++$r, --$c)} elsif ($dir == W) {return ($r, --$c)} elsif ($dir == NW) {return (--$r, --$c)} else { die "Inside 'step': invalid param\n" } }
This sub could easily be rewritten as a hash lookup (since it's just a big lookup table anyway):
our %step = ( N => { R => -1, C => 0 }, NE => { R => -1, C => +1 }, E => { R => 0, C => +1 }, SE => { R => +1, C => +1 }, S => { R => +1, C => 0 }, SW => { R => +1, C => -1 }, W => { R => 0, C => -1 }, NW => { R => -1, C => -1 } );
Then instead of this:
($rows, $cols) = step($dir, $rows, $cols);
use this:
$rows += $step{$dir}{R}; $cols += $step{$dir}{C};
For free, sub stepback is gone, and instead of the call use this:
$rows -= $step{$dir}{R}; $cols -= $step{$dir}{C};
[step and stepback are essentially the same.]

You don't need all of those use constant N => 0; either -- the string literal is just as good as a constant.

There are probably a few more optimizations that could be made. For instance, what if you're given a grid, but instead of "the" word list, your given a lexicon? Then in search, the inner loop:

for my $idx (0..$#words)
will take quite some time. [BTW, $#words doesn't refer to the sub's my $word, but to the my @words declared at file scope. Bonus points for knowing why ;) ]

The first optimization I would try would be to remove each entry from @words when the search is successful, and move the info to @words_found. Then later searches on @words would only look for the remaining words. This would also speed it up in the current incarnation, but perhaps only marginally.

-QM
--
Quantum Mechanics: The dreams stuff is made of


In reply to Re: Word Search Puzzle by QM
in thread Word Search Puzzle by Anonymous Monk

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.