首页 > 代码库 > 汉若塔问题的迭代解法

汉若塔问题的迭代解法

先看递归解法,用Perl语言一分钟不到就写完了。

sub hanno_recursive {
my ($from, $to, $reserve, $n) =  @_;
if (1 == $n) {
print "move $n from $from to $to\n";
return;
}

hanno_recursive($from, $reserve, $to, $n -1);
print "move $n from $from to $to\n";
hanno_recursive($reserve, $to, $from, $n -1);
}

极其简洁优美。充分体现了递归的优雅。


接下来,考虑迭代解法。考虑将问题分解为树结构。很显然, 将A   B  C看成一个圈, 则左右子树具有某种对称。即顺时针或逆时针旋转。

这样,我们完全可以通过左树求得右树, 问题变成为了线性递归, 这个很容易转换为迭代。

原理知道了, 写这个代码,还是比较费劲, 花了1个小时才调好。

sub hanno_iterate {
my ($from, $to, $reserve, $n) =  @_;
my @left = ();
my @right = ();
#move to leaf node
my $count = $n;
while ($count > 1) {
my $tmp = $to;
$to = $reserve;
$reserve = $tmp;
$count--;
}

for (my $index = 1; $index <= $n; $index++) {
my $new = "move $index from $from to $to\n";

push @left, $new;
while ($new = shift @right) {
push @left, $new;
}
last if ($index == $n);

if (($index % 2) == ($n % 2)){
# anti-clock $from -> $to, $reserve->$from, $to -> $reserve
foreach my $opt(@left) {
my $left_value = http://www.mamicode.com/"$opt";
$left_value =http://www.mamicode.com/~ tr/ABC/CAB/;
push @right, $left_value;
}
} else {
# clock $from -> $reserve, $reserve->$to, $to -> $from
foreach my $opt(@left) {
my $left_value = http://www.mamicode.com/"$opt";
$left_value =http://www.mamicode.com/~ tr/ABC/BCA/;
push @right, $left_value;
}
}
my $tmp = $to;
$to = $reserve;
$reserve = $tmp;
}

foreach my $opt(@left) {
print $opt;
}


}


汉若塔问题的迭代解法