in reply to find all paths of length n in a graph
But it's easy enough to calculate tile numbers from the co-ordinates in this loop if required. Note the optimisation where we don't bother to go into the deepest loop if we're off the top or bottom edge. You could also do it the opposite way, looping through tile numbers from 1 to ($p*$q) and then effectively calculating the x and y position of the current tile in order to work out whether you are near an edge, but this strikes me as likely to be slower as it involves more arithmetic.for (my $y = 1; $y <= $q; $y++) { for (my $x = 1; $x <= $p; $x++) { for (my $ny=$y-1; $ny<=$y+1; $ny++) { next if ($ny < 1 || $ny > $q); for (my $nx=$x-1; $nx<=$x+1; $nx++) { next if ($nx < 1 || $nx > $p || ($nx == $x && $ny == $y)); push @{$adjlist{"$x,$y"}}, [$nx,$ny]; } } } }
|
---|