Algorithm-Search

 view release on metacpan or  search on metacpan

t/15.t  view on Meta::CPAN

      {
        push @moves, [$self->{zero_at}->[0]-1,$self->{zero_at}->[1]];
      }
    }
    if ($self->{zero_at}->[0] < 3) {
      push @moves, [$self->{zero_at}->[0]+1,$self->{zero_at}->[1]];
    }
    if ($self->{zero_at}->[1] > 0) {
      if (!(
       $self->{locked}->[$self->{zero_at}->[0]]->[$self->{zero_at}->[1]-1]
       ))
      {
        push @moves, [$self->{zero_at}->[0],$self->{zero_at}->[1]-1];
      }
    }
    if ($self->{zero_at}->[1] < 3) {
      push @moves, [$self->{zero_at}->[0],$self->{zero_at}->[1]+1];
    }
    return @moves;
  }

  package main;
  use Algorithm::Search;
  my $puzzle = new fifteen;

  $puzzle->set_position(
'1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 0');

#  print "pvalue is ".$puzzle->value."\n";

  if (!($puzzle->distance_to_final_state)) {
#    print "s1 in final\n";
  }
  else {
#    print "s1 not in final\n";
  }

  $puzzle->set_position(
'1 2 3 4
5 6 7 8
9 10 11 12
13 14 0 15');

#  print "pvalue is ".$puzzle->value."\n";
  if (!($puzzle->distance_to_final_state)) {
#    print "s2 in final\n";
  }
  else {
#    print "s2 not in final\n";
  }

  my $fifteen_search = new Algorithm::Search();
  $fifteen_search->search({search_this=>$puzzle,
   solutions_to_find=>1,
   search_type => 'bfs'
  });
#  print "Solution found :\n";
#  use Data::Dumper;
#  print Dumper($fifteen_search->path)."\n";
  is_deeply ($fifteen_search->path,
   [
          [
            3,
            3
          ]
        ], 'one move problem');

#  $puzzle->set_position(
#'10 12 7 3
#5 13 8 4
#9 11 2 0
#15 14 1 6');
#
#  print "pvalue is ".$puzzle->value."\n";
#  $fifteen_search->search({search_this=>$puzzle,
#   solutions_to_find=>1,
#   search_type => 'bfs',
#   do_not_repeat_values => 1,
#   max_steps => 200000
#  });
#  print "NSolution found :\n";
#  use Data::Dumper;
#  print Dumper($fifteen_search->path)."\n";
#  is_deeply($fifteen_search->path,
#   [
#          [
#            2,
#            2
#          ],
#          [
#            3,
#            2
#          ],
#          [
#            3,
#            1
#          ],
#          [
#            2,
#            1
#          ],
#          [
#            2,
#            2
#          ],
#          [
#            1,
#            2
#          ],
#          [
#            1,
#            1
#          ],
#          [
#            2,
#            1
#          ],
#          [
#            2,
#            0
#          ],
#          [
#            1,
#            0
#          ],
#          [
#            1,
#            1
#          ],
#          [
#            0,
#            1
#          ],
#          [
#            0,
#            0
#          ],
#          [
#            1,
#            0
#          ],
#          [



( run in 0.661 second using v1.01-cache-2.11-cpan-39bf76dae61 )