smgfc has asked for the wisdom of the Perl Monks concerning the following question:
Well, in an attempt to kill my friends in word racer I set out to write a boggle solver. Before I got into the complex shapes used as word racer boards, I decided to figure out how to solve the simple 4X4 boggle board. I initally started by importing a dictionary and doing text preprocessing, but that seemed backward, and decided to concentrate on extracting words from the board first.
The first thing I did was create a sample program with a built in board and no UI or any superflous code. I decided to use a Depth First Search to find the possible strings of characters, and recursion combined with an array of moves to weed out all the possibilites. Essentially all that happens is the dfs sub starts with one character on the board and iterates through array moves, apply each transformation to the current x,y coordinate and branching a new dfs sub with the new coordinates.
With the current implementation there are a number of issues. The most pressing is a error I just don't know how to solve. When the dfs sub assigns whatever was passed to it I get this error: Odd number of elements in hash assignment at dfsboggle.txt line 30.. Since the only hash in the program is visited, and visited hasn't even been touched yet I just don't get it.
Another, more long term issue, has to do with memory. Every time I call dfs after the initial call I create a new visited hash to deal with the different branches and their respective visited nodes/characters. The same thing happens with the word scalar. I suppose, you could just pass a reference and at the end of each branch clear both visited and word, but if the program ever gets updated to do process the different branchs simultaneously then this will break. I dont know how much multi-threading would speed this up, if it is possible, or how to do it so the point is moot
Beyond that, obviously array board isn't passed by reference, but this is a sample program. I plan on keeping the moves in a closure with the dfs sub to avoid having to pass it. If that isnt a good idea I would love to here why, since I havent seen it done before. I haven't been able to test this because of the aforementioned problem, so the dfs sub could have problems that would output wrong answers. Any help or different approaches would be greatly appreciated. Code Below
#!/usr/bin/perl -w use strict; my (%visited); my ($word) = ""; my (@board) = ( ['a','b','c','d'], ['e','f','g','h'], ['i','j','k','l'], ['m','n','o','p'] ); my (@moves) = ( [0, -1], #x + 0, y + -1 [1, -1], [1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, 1] ); dfs(0, 0, %visited, $word); sub dfs { my ($x, $y, %visited, $word) = @_; my ($end) = 1; # end is used to determine if there are anymore cha +racters in the string before printing $word $visited{$x . $y}++; $word .= $board[$y][$x]; foreach (@moves) { next if $x + $$_[0] < 0 or $x + $$_[0] > 3 or $y + $$_[1] < 0 or $y + $$_[1] > 3 or exists $visited{ ($x + $$_[0]) . ($y + $$_[1]) }; #make sure the transformed coordinates are valid and ne +w $end = 0; dfs($x + $$_[0], $y + $$_[1], %visited, $word); } print $word if $end == 1; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Depth First Search and Boggle
by BrowserUk (Patriarch) on Aug 15, 2002 at 03:48 UTC | |
by smgfc (Monk) on Aug 15, 2002 at 05:39 UTC | |
by BrowserUk (Patriarch) on Aug 15, 2002 at 06:11 UTC | |
by the_slycer (Chaplain) on Aug 15, 2002 at 06:30 UTC | |
|
Re: Depth First Search and Boggle
by Abigail-II (Bishop) on Aug 15, 2002 at 09:26 UTC | |
by smgfc (Monk) on Aug 15, 2002 at 18:19 UTC | |
by Anonymous Monk on Aug 15, 2002 at 15:53 UTC | |
|
Re: Depth First Search and Boggle
by Abigail-II (Bishop) on Aug 15, 2002 at 16:38 UTC | |
by Sifmole (Chaplain) on Aug 15, 2002 at 17:07 UTC | |
by Abigail-II (Bishop) on Aug 15, 2002 at 17:23 UTC | |
by kelan (Deacon) on Sep 26, 2004 at 19:51 UTC | |
|
Re: Depth First Search and Boggle
by cLive ;-) (Prior) on Aug 15, 2002 at 04:38 UTC |