in reply to army building
One important question that I'm not quite clear on is whether you can have only one of each figure or more than one (points permitting).
Here's a simplistic approach that could work:
my @data = map +{ name => $_, value => $roster{$_} }, keys %roster; for (fit($maxpoints, 0)) { print join(" ", map $data[$_]->{name}, @$_), "\n"; } sub fit { my($points, $start) = @_; return () if $points <= 0; map { my $index = $_; $points < $data[$_]->{value} ? () : ([ $index ], map [ $index, @$_ ], fit($points - $data[$_]->{value}, $index ++ 1)) } $start .. $#data; }
This assumes each figure can appear only once; to allow figures to appear multiple times, replace $index + 1 in the recursive call with $index.
I don't have time to explain the code in detail right now, I'm afraid, but one thing to note is that this will include all permissible combinations, including "deficient" combinations, ie those to which you could add another figure without exceeding the points limit. Some of those can be filtered out by a slight modification to the map:
but I don't see an easy way to get rid of all of them with this approach. Hugomap { my $index = $_; $points < $data[$_]->{value} ? () : do { my @result = fit($points - $data[$_]->{value}, $index + 1); @result ? map([ $index, @$_ ], @result) : [ $index ] } $start .. $#data;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: army building
by dshahin (Pilgrim) on Mar 03, 2003 at 19:29 UTC |