P91 - Knight's tour.

AUTHOR

Edwin Pratomo

Specification

P91 (**) Knight's tour
       Another famous problem is this one: How can a knight jump on an NxN
       chessboard in such a way that it visits every square exactly once?
Hints: Represent the squares by pairs of their coordinates of the form
       X/Y, where both X and Y are integers between 1 and N. (Note that '/'
       is just a convenient functor, not division!) Define the relation
       jump(N,X/Y,U/V) to express the fact that a knight can jump from X/Y to
       U/V on a NxN chessboard. And finally, represent the solution of our
       problem as a list of N*N knight positions (the knight's tour).
use v6;



my $n = 5;
my $size = $n * $n;

my @track;
my @directions = flat ((1, -1 X 2, -2), (2, -2 X 1, -1));

sub valid_moves($curr, @temp_track=@track) {
    my @valid_squares = @directions.map(->$a,$b { ($curr.key + $a) => ($curr.value + $b) }).grep({0 <= all(.key, .value) < $n});
    # exclude occupied squares. !eqv doesn't work yet.
    @valid_squares.grep({ not $_ eqv any(|@temp_track, $curr) });
}

sub knight($square) {
    @track.push($square);
    return 1 if @track.elems == $size;

    # simple heuristic, for move ordering
    my @possible_moves = valid_moves($square).sort: ->$a,$b {
        valid_moves($a, [|@track,$a]).elems <=> valid_moves($b, [|@track, $b]).elems
            or $a.key <=> $b.key
            or $a.value <=> $b.value;
    };

    return unless @possible_moves.elems;

    for @possible_moves -> $try {
        my $result = knight($try);
        if $result {
            return 1;
        }
        else {
            @track.pop;
        }
    }
}

if knight(0 => 0) {
    say "FOUND: " ~ @track.perl;
}
else {
    say "NOT FOUND";
}

# vim: expandtab shiftwidth=4 ft=perl6

See Also

P01-scottp.pl

P01 - Find the last box of a list.

P01-topo.pl

P01 - Find the last element of a list.

P02-scottp.pl

P02 - Find the last but one box of a list.

P02-topo.pl

P02 - Find the last two elements of a list.

P03-scottp.pl

P03 - Find the K'th element of a list.

P03-topo.pl

P03 - Find the kth element of a list.

P04-scottp.pl

P04 - Find the number of elements of a list

P04-topo.pl

P04 - Find the number of elements in a list.

P05-scottp.pl

P05 - Reverse a list

P05-topo.pl

P05 - Reverse a list.

P06-ajs.pl

P06 - Find out whether a list is a palindrome.

P06-scottp.pl

P06 - Find out whether a list is a palindrome.

P06-topo.pl

P06 - Find out whether a list is a palindrome.

P07-eric256.pl

P07 - Flatten a nested array structure.

P07-topo.pl

P07 - Flatten a nested array structure.

P07-viklund.pl

P07 - Flatten a nested array structure.

P08-eric256.pl

P08 - Eliminate consecutive duplicates of list elements.

P08-topo.pl

P08 - Eliminate consecutive duplicates of list elements.

P08-viklund.pl

P08 - Eliminate consecutive duplicates of list elements.

P09-rje.pl

P09 - Pack consecutive duplicates of list elements into sublists.

P09-scottp.pl

P09 - Pack consecutive duplicates of list elements into sublists.

P09-topo.pl

P09 - Pack consecutive duplicate elements of a list into sublists.

P09-unobe.pl

P09 - Pack consecutive duplicates of list elements into sublists.

P10-scottp.pl

P10 - Run-length encoding of a list.

P10-topo.pl

P10 - Run-length encoding of a list.

P10-unobe.pl

P10 - Run-length encoding of a list.

P11-topo.pl

P11 - Modified run-length encoding.

P11-unobe.pl

P11 - Modified run-length encoding.

P12-rhebus.pl

P12 - Decode a run-length encoded list.

P12-topo.pl

P12 - Decode modified run-length encoding.

P12-unobe.pl

P12 - Decode a run-length encoded list.

P13-rhebus.pl

P13 - Run-length encoding of a list (direct solution).

P13-topo.pl

P13 - Direct run-length encoding.

P13-viklund.pl

P13 - Run-length encoding of a list (direct solution).

P14-scottp.pl

P14 - Duplicate the elements of a list.

P14-topo.pl

P14 - Duplicate the elements in a list.

P14-viklund.pl

P14 - Duplicate the elements of a list.

P15-rhebus.pl

P15 - Replicate the elements of a list a given number of times.

P15-topo.pl

P15 - Replicate the elements of a list a given number of times.

P15-unobe.pl

P15 - Replicate the elements of a list a given number of times.

P16-edpratomo.pl

P16 (**) Drop every N'th element from a list.

P16-topo.pl

P16 - Drop every nth element from a list.

P17-topo.pl

P17 - Split a list into two parts; the length of the first part is given.

P17-unobe.pl

P17 - Split a list into two parts; the length of the first part is given.

P18-topo.pl

P18 - Extract a slice from a list. Indices start at 1.

P19-topo.pl

P19 - Rotate a list n places to the left.

P20-rhebus.pl

P20 - Remove the K'th element from a list.

P20-topo.pl

P20 - Remove the kth element of a list.

P21-scottp.pl

P21 - Insert an element at a given position into an array.

P21-topo.pl

P21 - Insert an element at a given position into a list.

P22-scottp.pl

P22 - Create a list containing all integers within a given range.

P22-topo.pl

P22 - Create a list containing all integers within a given range.

P23-topo.pl

P23 - Extract a given number of randomly selected elements from a list.

P24-topo.pl

P24 - Draw N different random numbers from the set 1..M.

P25-topo.pl

P25 - Generate a random permutation of the elements of a list.

P26-topo.pl

P26 - Generate the combinations of k distinct objects chosen from the n elements of a list.

P31-rhebus.pl

P31 - Determine whether a given integer number is prime.

P32-rhebus.pl

P32 - Determine the greatest common divisor of two positive integer

P33-rhebus.pl

P33 - Determine whether two positive integer numbers are coprime.

P34-rhebus.pl

P34 - Calculate Euler's totient function phi(m).

P35-rhebus.pl

P35 - Determine the prime factors of a given positive integer.

P36-ovid.pl

P36 - Determine the prime factors of a given positive integer (2).

P36-rhebus.pl

P36 - Determine the prime factors of a given positive integer (2).

P37-rhebus.pl

P37 - Calculate Euler's totient function phi(m) (improved).

P39-rhebus.pl

P39 - A list of prime numbers.

P40-rhebus.pl

P40 - Goldbach's conjecture.

P41-rhebus.pl

P41 - A list of Goldbach compositions.

README.md

The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society. All rights reserved.