Pathologically Eclectic Rubbish Lister PerlMonks

### Re: Hanoi Challenge

by ambrus (Abbot)
 on Oct 23, 2004 at 18:33 UTC ( #401870=note: print w/replies, xml ) Need Help??

Here's my solution.

It's surely not optimal, but it's a very basic extension to the 3-peg solution, so the code is very short. It uses O(d1/(p-3)) moves O(2d/(p-2)) moves for the solution with d disks and p pegs (I'm too lazy to calculate the exact numbers).

(Updated fomula again. The O sign is meant if p is constant but d->inf)

Update: this is probably very suboptimal for more than 4 pegs.

```#!/usr/local/bin/perl

use warnings; use strict;

\$ARGV[0] =~ /(\d+)/ or die;
my \$pegs = \$1;
\$ARGV[1] =~ /(\d+)/ or die;
my \$disks = \$1;

{
my @pegnames = "A" .. "Z";
sub printmove {
my(\$d, \$f, \$t) = @_;
print \$d, ": ", \$pegnames[\$f], " -> ", \$pegnames[\$t], "\n"
}
}

sub rec {
my(\$n, \$s, \$d, \$t, @o) = @_;
\$n > 0 or return;
my \$k = @o < \$n - 1 ? @o : \$n - 1;
#warn "[(\$n:\${\(\$n-\$k)} \$s->\$d]\n";
rec(\$n - \$k - 1, \$s, \$t, \$d, @o);
printmove(\$n - \$k + \$_, \$s, \$o[\$_]) for
0 .. \$k - 1;
printmove(\$n, \$s, \$d);
printmove(\$n - \$k + \$_, \$o[\$_], \$d) for
reverse(0 .. \$k - 1);
rec(\$n - \$k - 1, \$t, \$d, \$s, @o);
#warn "[)]\n"
}

rec(\$disks, 0, 1, 2 .. \$pegs - 1);

__END__

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://401870]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2022-01-29 11:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
In 2022, my preferred method to securely store passwords is:

Results (74 votes). Check out past polls.

Notices?