Skip to content
Browse files

Change API of solve.

  • Loading branch information...
1 parent 38d9d2e commit e01604d75620099570e5b6d0e343252fb1e84e24 @hiratara committed Sep 26, 2012
Showing with 32 additions and 24 deletions.
  1. +27 −1 ex/simpath.pl
  2. +5 −23 lib/Algorithm/Simpath.pm
View
28 ex/simpath.pl
@@ -2,12 +2,38 @@
use warnings;
use Algorithm::Simpath;
+sub grid_edges($$) {
+ my ($width, $height) = @_;
+
+ my @edges;
+ for my $n (0 .. $width + $height - 1) {
+ for my $x (0 .. $n) {
+ my $y = $n - $x;
+ $x < $width && $y < $height or next;
+ push @edges, ["$x,$y", ($x + 1) . ",$y"] if $x + 1 < $width;
+ push @edges, ["$x,$y", "$x," . ($y + 1)] if $y + 1 < $height;
+ }
+ }
+
+ @edges;
+}
+
sub count($); sub count($) {
my $node = shift;
return 0 unless $node;
return 1 unless ref $node;
$node->{count} //= count($node->{low}) + count($node->{high});
}
-my $zdd = solve(9, 9);
+my $width = ($ARGV[0] // 4) + 1;
+my $height = ($ARGV[1] // 4) + 1;
+
+my $start = "0,0";
+my $goal = sprintf "%d,%d", $width - 1, $height - 1;
+
+my $zdd = solve(
+ start => $start,
+ goal => $goal,
+ edges => [grid_edges $width, $height],
+);
print +(count $zdd), "\n";
View
28 lib/Algorithm/Simpath.pm
@@ -5,22 +5,6 @@ our $VERSION = '0.01';
use Exporter qw(import);
our @EXPORT = qw(solve);
-sub grid_edges($$) {
- my ($width, $height) = @_;
-
- my @edges;
- for my $n (0 .. $width + $height - 1) {
- for my $x (0 .. $n) {
- my $y = $n - $x;
- $x < $width && $y < $height or next;
- push @edges, ["$x,$y", ($x + 1) . ",$y"] if $x + 1 < $width;
- push @edges, ["$x,$y", "$x," . ($y + 1)] if $y + 1 < $height;
- }
- }
-
- @edges;
-}
-
sub low_node($$) {
my ($node, $grid_edge) = @_;
@@ -84,14 +68,12 @@ sub node_id($) {
join "\t", map {"$_-" . ($mate->{$_} // '')} sort keys %$mate;
}
-sub solve($$) {
- my ($col_width, $col_height) = @_;
- my $width = $col_width + 1;
- my $height = $col_height + 1;
+sub solve(@) {
+ my %params = @_;
+ my $start = delete $params{start} // die "no start";
+ my $goal = delete $params{goal} // die "no goal";
+ my @grid_edges = @{delete $params{edges} // []};
- my $start = "0,0";
- my $goal = sprintf "%d,%d", $width - 1, $height - 1;
- my @grid_edges = grid_edges $width, $height;
my %route_left;
for (@grid_edges) {
$route_left{$_}++ for @$_;

0 comments on commit e01604d

Please sign in to comment.
Something went wrong with that request. Please try again.