Commit 35371055 by PotatoGim

재미로 해본 BFS 예제

parent a70ac3c9
#!/usr/bin/env perl
#=============================================================================
# FILE: bfs_example.pl
# USAGE: ./bfs_example.pl
# BRIEF: BFS(너비 우선 탐색) 구현 예제
# AUTHOR: PotatoGim <potatogim@potatogim.net>
# DATE: 01/20/2015 11:04:19 AM
# VERSION: 1.0
# LICENSE: Artistic License
# NOTE:
#=============================================================================
use strict;
use warnings;
use utf8;
use Data::Dumper;
$Data::Dumper::Terse = 1;
$Data::Dumper::Indent = 0;
my %root = (
"a" => 1,
"b" => 2,
"c" => ["c1", "c2", "c3"],
"d" => { "a" => "d1", "b" => "d2" },
"e" => 2,
);
my @queue = ({parent => "root", value => \%root});
# DFS가 아니라 BFS.
while (my $node = shift(@queue))
{
printf "NODE: %s - %s\n", ref($node), Dumper($node);
my $parent = $node->{parent};
my $value = $node->{value};
if (ref($value) eq "HASH")
{
push (@queue
, {
parent => "$parent.$_",
value => $value->{$_},
}
) foreach (keys(%{$value}));
}
elsif (ref($value) eq "ARRAY")
{
my $i = 0;
push (@queue
, {
parent => "$parent\[" . $i++ . "]",
value => $_,
}
) foreach (@{$value});
}
elsif (ref($value) eq "SCALAR")
{
push (@queue
, {
parent => $parent,
value => $$value,
}
);
}
elsif (!length(ref($value)))
{
print "VALUE: $value($parent)\n";
}
}
1;
__END__
=encoding utf8
=head1 NAME
bfs_example.pl - BFS(너비 우선 탐색) 구현 예제
=head1 SYNOPSIS
=head1 DESCRIPTION
=over
=item B<Item 1>
=item B<Item 2>
=item B<Item 3>
=back
=head1 AUTHOR
PotatoGim <potatogim@potatogim.net>
=head1 CONTRIBUTORS
=head1 COPYRIGHT AND LICENSE
Copyright(c) 2015 by PotatoGim <potatogim@potatogim.net>
This is free software; you can redistribute it and/or modify it
under the same terms as Perl 5 itself at:
L<http://www.perlfoundation.org/artistic_license_2_0>
You may obtain a copy of the full license at:
L<http://www.perl.com/perl/misc/Artistic.html>
=head1 SEE ALSO
=cut
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment