my @candidates; if($this_place == 0) { @candidates = @placements; } else { my $mask = $zero; if(my $top = $positions[$this_place]->[0]) { vec($mask, $top - 1, 4) = 15; } if(my $left = $positions[$this_place]->[3]) { vec($mask, $left - 1, 4) = 15; } my $wanted = ($state & $mask) ^ $mask; @candidates = grep { ($_ & $mask) eq $wanted and ($state & $_) eq $zero } @placements; } foreach (@candidates) { solve($state | $_, @placed, $_); }