Jump to content

I'm a software engineer, get me out of here: Difference between revisions

m
→‎{{header|Perl}}: a little less terse
m (→‎{{header|Perl}}: eliminate hard-coded value)
m (→‎{{header|Perl}}: a little less terse)
Line 792:
$w++;
 
sub xy to_xy { "my(@{[$i) = shift; '(' . join(',', int ($_[0] i/ $w ]}),@{[ $_[0] i% $w) . ]}')"' }
sub fromxyfrom_xy { my($x,$y) = @_[0]; $x * $w + $_[1]y }
 
my @directions = ( 1, -1, -$w-1 .. -$w+1, $w-1 .. $w+1 );
my $startpos = fromxyfrom_xy 11, 11;
 
my @nodes;
push @nodes, $-[0] while $d =~ /\d/g;
my %dist = map { $_ => alldestall_destinations($_) } @nodes; # all shortest from-to paths
 
my @best;
Line 808:
my $n = $best[$short] =~ tr/\n//;
print "shortest escape routes ($n) of length $short:\n",
$best[$short] =~ s/\d+/ xyto_xy $& /ger;
 
print "\nshortest from (21,11) to (1,11):\n\t",
$dist{fromxyfrom_xy 21, 11}{fromxyfrom_xy 1, 11} =~ s/\d+/ xyto_xy $& /ger, "\n";
print "\nshortest from (1,11) to (21,11):\n\t",
$dist{fromxyfrom_xy 1, 11}{fromxyfrom_xy 21, 11} =~ s/\d+/ xyto_xy $& /ger, "\n";
 
@best = ();
$best[tr/ //] .= "\t$_\n" for map { values %$_ } values %dist;
print "\nlongestshortest paths (length $#best) :\n",
$best[-1] =~ s/\d+/ xyto_xy $& /ger;
 
my @notreach = grep !$dist{$startpos}{$_}, @nodes;
print "\nnot reached from HQ:\n\t@notreach\n" =~ s/\d+/ xyto_xy $& /ger;
 
@best = ();
$best[tr/ //] .= "\t$_\n" for values %{ $dist{$startpos} };
print "\nlongest reinforcement from HQ is $#best for:\n",
$best[-1] =~ s/\d+/ xyto_xy $& /ger;
 
sub all_destinations
sub alldest
{
my @queue = shift;
2,392

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.