Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
35
Task/Topswops/Perl/topswops.pl
Normal file
35
Task/Topswops/Perl/topswops.pl
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
sub next_swop {
|
||||
my( $max, $level, $p, $d ) = @_;
|
||||
my $swopped = 0;
|
||||
for( 2..@$p ){ # find possibilities
|
||||
my @now = @$p;
|
||||
if( $_ == $now[$_-1] ) {
|
||||
splice @now, 0, 0, reverse splice @now, 0, $_;
|
||||
$swopped = 1;
|
||||
next_swop( $max, $level+1, \@now, [ @$d ] );
|
||||
}
|
||||
}
|
||||
for( 1..@$d ) { # create possibilities
|
||||
my @now = @$p;
|
||||
my $next = shift @$d;
|
||||
if( not $now[$next-1] ) {
|
||||
$now[$next-1] = $next;
|
||||
splice @now, 0, 0, reverse splice @now, 0, $next;
|
||||
$swopped = 1;
|
||||
next_swop( $max, $level+1, \@now, [ @$d ] );
|
||||
}
|
||||
push @$d, $next;
|
||||
}
|
||||
$$max = $level if !$swopped and $level > $$max;
|
||||
}
|
||||
|
||||
sub topswops {
|
||||
my $n = shift;
|
||||
my @d = 2..$n;
|
||||
my @p = ( 1, (0) x ($n-1) );
|
||||
my $max = 0;
|
||||
next_swop( \$max, 0, \@p, \@d );
|
||||
return $max;
|
||||
}
|
||||
|
||||
printf "Maximum swops for %2d cards: %2d\n", $_, topswops $_ for 1..10;
|
||||
Loading…
Add table
Add a link
Reference in a new issue