Knight's tour: Difference between revisions

Content added Content deleted
(Improved D code)
(→‎{{header|Perl}}: minor cleanup)
Line 471: Line 471:
=={{header|Perl}}==
=={{header|Perl}}==
Knight's tour using [[wp:Knight's_tour#Warnsdorff.27s_algorithm|Warnsdorffs algorithm]]
Knight's tour using [[wp:Knight's_tour#Warnsdorff.27s_algorithm|Warnsdorffs algorithm]]
<lang perl># Find a knight's tour
<lang perl>use strict;
use warnings;
# Find a knight's tour


my @board;
# The map ensures that each row gets its own array,
# rather than all being references to a single one.
my @board = map { [ @{[ @$_ ]} ] } ( [ (0) x 8 ] ) x 8;


# Choose starting position - may be passed in on command line; if
# Choose starting position - may be passed in on command line; if
Line 556: Line 556:
my $square = shift;
my $square = shift;
return unless $square =~ /^([a-h])([1-8])$/;
return unless $square =~ /^([a-h])([1-8])$/;
$j = ord($1) - ord('a'); $i = 8 - $2;
return (8-$2, ord($1) - ord('a'));
return ($i, $j);
}</lang>
}</lang>