Slashdot Mirror


Solving the Knight's Tour Puzzle In 60 Lines of Python

ttsiod writes "When I was a kid, I used to play the Knight's Tour puzzle with pen and paper: you simply had to pass once from every square of a chess board, moving like a Knight. Nowadays, I no longer play chess; but somehow I remembered this nice little puzzle and coded a 60-line Python solver that can tackle even 100x100 boards in less than a second. Try beating this, fellow coders!"

22 of 311 comments (clear)

  1. awesome by sofar · · Score: 5, Funny

    too bad that your code will break with the next python version.

    1. Re:awesome by Falstius · · Score: 4, Funny

      Thanks for that link, the new print command makes a lot more sense than the old one.

  2. Easy by Anonymous Coward · · Score: 5, Funny

    C-x C-m KnightsPuzzle

  3. What would Michael Knight do? by Anonymous Coward · · Score: 4, Funny

    He'd hop into KITT and go anywhere he damn well pleases.

  4. Re:OK by $RANDOMLUSER · · Score: 3, Funny

    I blow my nose at you, so-called "Arthur King", you and all your silly English K-nigg-its.

    --
    No folly is more costly than the folly of intolerant idealism. - Winston Churchill
  5. Re:All done. by Anonymous Coward · · Score: 5, Funny

    Except you commented out all of the code.

  6. Python has "perfect syntax"? by Anonymous Coward · · Score: 1, Funny

    Please... Why do we need slashvertisements for programming languages on Slashdot?

  7. Re:All done. by somenickname · · Score: 5, Funny

    There. I did it in one line of code.

    That doesn't look like perl to me...

  8. OMG by l3v1 · · Score: 3, Funny

    Now let's see, they taught us about this problem back when I was a six- or seven-grader (~'90-91, can't recall exactly) as one of the illustrations for backtracking (yes, I know we can do it without backtracking, that was not the point then I guess). Go figure.

    --
    I am putting myself to the fullest possible use, which is all I can think that any conscious entity can ever hope to do.
  9. Re:A trivial backtracking algorithm and... by jellomizer · · Score: 5, Funny

    Of course like all other programmers he thinks he is better then everyone else.

    --
    If something is so important that you feel the need to post it on the internet... It probably isn't that important.
  10. Re:All done. by gparent · · Score: 4, Funny

    Woosh.

  11. Re:All done. by Kingrames · · Score: 4, Funny

    From experience, commenting out the code makes it better. :P

    --
    If you can read this, I forgot to post anonymously.
  12. Re:All done. by svank · · Score: 3, Funny

    In my experience, every line commented out makes the program run faster. My programs tend to run instantly if I comment the entire thing! Using a # must invoke an incredible optimization engine! Why don't they use it by default?

  13. Knuth by /dev/trash · · Score: 2, Funny

    He's preached against premature optimization for years.

    1. Re:Knuth by pizzach · · Score: 2, Funny

      Holy crap! My prof said that comments should make no difference in the speed that the program runs at, but your code is blazing fast!

      --
      Once you start despising the jerks, you become one.
  14. Re:better algo by seann · · Score: 5, Funny

    Wait a second. What's wrong with recursion?

    --
    I'm a big retard who forgot to log out of Slashdot on Mike's computer! LOOK AT ME.
  15. Re:printf by Anonymous Coward · · Score: 4, Funny

    No output, and your font is all wrong.

    *ducks*

  16. Re:printf by Anonymous Coward · · Score: 3, Funny

    It will run 1/10th the speed of Python?

  17. 1 line from the future (.NET 4.0) by saddino · · Score: 2, Funny

    object[] finalBoard = System.Math.KnightsTour(64);

  18. Re:printf by earthbound+kid · · Score: 3, Funny

    You misspelled puts? ;-D

  19. Half in size-Double in speed...with Perl by evariste.galois · · Score: 2, Funny

    Can't copy n' paste the code here because I get the message: "Try not to use so many 'junky' words"! So, this is how Slashdot behaves to the language that brought it to life. Anyway,you can find the Perl script here.

  20. Re:better algo by Anonymous+Brave+Guy · · Score: 2, Funny

    I don't know. What's wrong with recursion?

    --
    If you disagree, post your argument. (-1, Overrated) isn't your personal censorship tool for views you don't like.