Algorithm-Search
view release on metacpan or search on metacpan
$self->{tree} = $self->{tree}->{$letter};
$self->{word} .= $letter;
return 0;
}
sub is_solution {
my $self = shift;
if ($word_list{$self->{word}}) {
return 1;
}
return 0;
}
sub distance_to_final_state {
my $self = shift;
if ($word_list{$self->{word}}) {
#print STDERR "Found word ".$self->{word}."\n";
return 0;
}
return 1;
}
sub copy {
my $self = shift;
my $copy = $self->new;
$copy->{tree} = $self->{tree};
$copy->{word} = $self->{word};
$copy->{column} = $self->{column};
$copy->{row} = $self->{row};
$copy->{max_row} = $self->{max_row};
$copy->{max_column} = $self->{max_column};
$copy->{board} = $self->{board};
for my $i (0..$self->{max_row}) {
for my $j (0..$self->{max_column}) {
$copy->{chosen}->[$i]->[$j] = $self->{chosen}->[$i]->[$j];
}
}
return $copy;
}
sub next_moves {
my $self = shift;
return ('UL','U','R','D','L','UR','DR','DL');
}
package main;
my @solutions;
my $sol_count;
use Algorithm::Search;
my $board =
"TEA
SEA
ART";
my @rows = split /\n/,$board;
my $columns = length($rows[0])-1;
my %words_found;
my $puzzle_search = new Algorithm::Search();
for my $i (0..$#rows) {
for my $j (0..$columns) {
my $puzzle = new words;
$puzzle->set_position($i,$j,$board);
$puzzle_search->search({search_this=>$puzzle,
max_steps=>3000,
solutions_to_find=>0,
distance_can_increase=>1,
no_value_function => 1,
search_type => 'bfs'
});
foreach my $sol ($puzzle_search->solutions) {
$words_found{$sol->{word}} = 1;
}
# print STDERR "steps taken ".$puzzle_search->steps."\n";
}
}
# print STDERR "Words found ".join("\n",sort keys %words_found)."\n\n";
is(join("\n",sort keys %words_found)."\n",
"ARE
AREA
ART
ASEA
AT
ATE
EAR
EARS
EASE
EAST
EAT
EATER
EATERS
ERA
ERAS
ERASE
ERST
ESTER
RATE
RATES
RESEAT
RESET
REST
SEA
SEAR
SEAT
SEE
SEER
SERA
SET
STEER
STET
TAR
TARS
TEA
TEAR
( run in 0.790 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )