I don't know what the rules of this game are, but the code below (31 lines) appears to work...

It ran 100x100, from (0,0) to (97, 92) in 0.33secs on my machine, where the OP code did the same in 0.87. Of course I have cheated and removed all the subroutines and changed the way the edges of the board are detected :-)

use strict ; use warnings ; use Time::HiRes qw(clock_gettime CLOCK_PROCESS_CPUTIME_ID) ; my $t0 = clock_gettime(CLOCK_PROCESS_CPUTIME_ID) ; my ($N, $x0, $y0, $VERBOSE) = @ARGV ; if ((@ARGV < 4) || ($N < 4) || ($x0 >= $N) || ($y0 >= $N)) { die "Use: SIZE [4..] START_X [0..SIZE-1] START_Y [0..SIZE-1] [0,1]\n +" ; } ; my @moves = ([-2,-1], [-1,-2], [-2 ,1],[-1 ,2], [2 ,-1], [1, -2],[2, 1 +],[1, 2]) ; my @board = ( [(2, 3, (4) x ($N-4), 3, 2, 0, 0)], [(3, 4, (6) x ($N-4) +, 4, 3, 0, 0)], map( { [(4, 6, (8) x ($N-4), 6, 4, 0, 0)] } (1..$N-4) ), [(3, 4, (6) x ($N-4), 4, 3, 0, 0)], [(2, 3, (4) x ($N-4) +, 3, 2, 0, 0)], [(0) x ($N+2)], [(0) x ($N+2)] ) ; for my $move (1..($N * $N) - 1) { my ($MIN, $x, $y) = (10, undef, undef) ; foreach (@moves) { my $vr = \$board[$x0 + $_->[0]][$y0 + $_->[1]] ; next if ($$vr == 0) || (--$$vr >= $MIN) ; ($MIN, $x, $y) = ($$vr, $x0 + $_->[0], $y0 + $_->[1]) ; } ; die "Stuck at ($x0, $y0) at move $move\n" unless defined($x) ; print "$move: ($x0, $y0) -> ($x, $y)\n" if $VERBOSE ; ($board[$x0][$y0], $x0, $y0) = (0, $x, $y) ; } ; print "Ended at ($x0, $y0) in ", clock_gettime(CLOCK_PROCESS_CPUTIME_I +D) - $t0, "\n" ;

In reply to Re: Knight's Tour Problem in Perl by gone2015
in thread Knight's Tour Problem in Perl by ptoulis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.