First issue. Why do you have the ability to store multiple words with the exact same characters? That is a complication that can just lead to duplicate solutions. Instead I'd naturally assign to the is_word element. Unless I was clever and assigned the word to the word element.

A stylistic issue. I'd recursively traverse the character to add words. Using eval there is an error-prone sledgehammer. What happens with words that include single quotes? You're trusting your input in a way that I avoid.

One minor note. Instead of a breadth-first search, my natural inclination would be a depth-first recursive search, and then I could do a local $seen{$pos} = 1; to mark a node seen. That's not better or worse than what you did, just different. But it does get rid of the queue management. Here is untested code to show you what that bit could look like (note that I am assuming the cleverness of putting the actual word in the word slot):

for my $pos (keys %map) { add_solutions($pos, $dict); } my %seen; sub add_solutions { my ($node, $tree) = @_; local $seen{$node} = 1; my $char = $board{$node}; $tree = $tree->{$char} or return; push @solutions, $tree->{word} if $tree->{word}; for my $new_node (grep not $seen{$_}, @{$map{$node}}) { add_solutions($new_node, $tree); } }

In reply to Re: Improve My FaceBook Scramble Solver by tilly
in thread Improve My FaceBook Scramble Solver by Limbic~Region

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.