Next | Trivial Utilities | 117 |
sub conversion_path { # Do BFS on configuration information my ($s, $d) = @_; return ($s) if $s eq $d; my @queue = ($s); my %conversions; for (keys %CONF) { push @{$conversions{$1}}, $2 if /(.*)->(.*)/; } my %good_path_to = ($s => []); while (@queue) { my ($here) = shift @queue; for my $next_type (@{$conversions{$here}}) { next if exists $good_path_to{$next_type}; # we've been here already my $p = [@{$good_path_to{$here}}, $here]; return @$p, $next_type if $next_type eq $d; $good_path_to{$next_type} = $p; push @queue, $next_type; } } # no path found return; }
@queue is a list of places that the function knows how to get to
$good_path_to{foo} says how to get from $s to foo
At each step, it investigates the places reachable from the place at the head of the queue
If it arrives at the intended destination, it returns
Next | Menu | Copyright © 2012 M. J. Dominus |