Forgot your password?
typodupeerror
Programming Math IT Technology Entertainment Games

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

Posted by Soulskill
from the snakes-and-horsies dept.
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!"
This discussion has been archived. No new comments can be posted.

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

Comments Filter:
  • better algo (Score:5, Informative)

    by Coneasfast (690509) on Sunday November 30, 2008 @03:12PM (#25935203)

    Apparently, this isn't NP-complete. There is an algorithm that can solve this in O(n) time, see here: http://mathworld.wolfram.com/KnightsTour.html [wolfram.com]

    This will save a LOT of time for larger boards. Try to implement this.

  • Re:awesome (Score:1, Informative)

    by Anonymous Coward on Sunday November 30, 2008 @03:36PM (#25935485)

    And the use of xrange()

  • Re:Perl (Score:5, Informative)

    by berend botje (1401731) on Sunday November 30, 2008 @03:39PM (#25935517)
    I thought you were being a smart-ass. You weren't:

    Chess::Piece::Knight [cpan.org].

    Perl is awesome!

  • by Free the Cowards (1280296) on Sunday November 30, 2008 @03:46PM (#25935573)

    Alas, Python lambdas are very limited, only allowing a single expression. If you need a function that does two things, you can't use lambda anymore. This is not a great hardship as Python allows you to declare inner-scoped functions and you can use that instead, but it's still annoying. I do recommend Python though, as it's a great language even with the occasional shortcoming.

  • Re:evolve or die (Score:2, Informative)

    by Anonymous Coward on Sunday November 30, 2008 @03:50PM (#25935621)

    Yawn.

    Another claim that Perl is "in its last throes".

  • Re:Easy (Score:5, Informative)

    by RAMMS+EIN (578166) on Sunday November 30, 2008 @04:29PM (#25935983) Homepage Journal

    While your humor is appreciated, I feel compelled to point out that it should be

    M-x knights-puzzle

    Lisp doesn't use CamelCase.

  • by harry666t (1062422) <harry666tNO@SPAMgmail.com> on Sunday November 30, 2008 @04:35PM (#25936021)
    > If you need a function that does two things, you can't use lambda anymore.

    wtf? of course you could!

    lambda x,y: (spam(x), meth(y)) [-1]

    yeah, less readable etc but works. And yes, Python is even capable of executing interesting, obscure one-liners:

    http://mobile.slashdot.org/comments.pl?sid=1022735&cid=25689627
  • Re:Perl (Score:2, Informative)

    by Anonymous Coward on Sunday November 30, 2008 @07:04PM (#25937375)

    (Note: I'm the GP AC.)

    Heh. That's funny, but actually, I *was* being a smartass. Don't get me wrong, I love Perl, but the example above is completely made up. (And looking into the Chess package on CPAN, it appears that there is no preimplemented way to generate knight's tours, either.)

    Still, I'm glad to hear that Perl's reputation for there being a module for anything and everything on CPAN still lives, at least. ;)

  • Re:awesome (Score:2, Informative)

    by earthbound kid (859282) on Sunday November 30, 2008 @07:11PM (#25937433) Homepage

    I got it to run in Python 3, and here are the changes I need to make:

    1) The file was screwed up and used a tab instead of 8 spaces (a problem unrelated to Python 3).
    2) I had to change all the print statements into print functions by wrapping the argument in parentheses.
    3) I had to change xrange to range.
    4) I had to add from functools import reduce to the top of the file.

    Done. 4 changes made in 5 minutes, the hardest of which (#1) would have screwed up Python 2.x as well.

  • MAJOR version (Score:3, Informative)

    by CarpetShark (865376) on Sunday November 30, 2008 @07:47PM (#25937725)

    The code will break with the next MAJOR version, not revision. That's entirely normal -- it's actually pretty much what version (as opposed to revision) means.

  • by harry666t (1062422) <harry666tNO@SPAMgmail.com> on Sunday November 30, 2008 @07:55PM (#25937793)
    Not doable.

    The colon in lambda is like a guy with a Vista laptop on a LUG meeting. The whole lambda syntax construct is an expression, and def's, if's, while's and all the other guys begin a block of statements (and are statements themselves). In Python, you simply cannot cleanly embed a statement within an expression -- you'd need braces of some kind (and suggesting that to the devs would be like asking to be crucified). Just think, how would this look like:

    map(lambda x:
      if abs(x) > 5:
        spam(x)
    , [3,-3,5,-5])

    With braces, you could write this like map(lambda x {if abs(x) > 5 {spam(x)}}, [3,-3,5,-5]), but well, try "from __future__ import braces".

    And btw, what is the return value of the if statement?... -- exactly.

    Just define a regular function :)

    ==

    But it's still cool to sometimes try and think why Guido or others haven't yet implemented a seemingly obvious feature X. Here's an interesting post about explicit self:

    http://neopythonic.blogspot.com/2008/10/why-explicit-self-has-to-stay.html

    (oh, and BTW: list comprehensions, "x if b else y", other lambdas, are all expressions, so if you'd really want to, you can still do a lot with lambdas (loops, fancy nested if's, etc) -- you may even wrap most of the statements (if, while, import, try, exec) into functions and then try to write whole program as a one great expression... Lisp-style. Just for fun ^^)
  • by setagllib (753300) on Sunday November 30, 2008 @07:58PM (#25937817)

    I don't know if you're just being ironic, but your Java code isn't even correct with respect to AWT/Swing threading semantics. You're supposed to create Swing objects only in the AWT event loop thread, using EventQueue.runLater() or one of its wrappers. I guess you're using Thread.sleep() later to get around the threading bugs you just created, but since your code is uncommented it's hard to tell.

  • Re:better algo (Score:4, Informative)

    by DarkOx (621550) on Sunday November 30, 2008 @08:24PM (#25938041) Journal

    Nothing is wrong with it at all. In a traditional compiled language its often the most efficent way to write something and it often gets you the most efficent compiled code and when it does not most compilers will be smart enough to build a loop construct when the go to the assembler stage.

    If you are using an interpreted langague like python or my favoirte Dialect( needs more respect ) then you have to be a little careful with it because these have a "soft stack" that can only get so deep. They have to keep track of how deep they are and where that have been, rather then just a beq and jmp on some register value, so they know what to do next. Mosty interpreters have a fixed stack depth although some manage to abstact this to linked list like structures internally and can keep going until the heap is exhasted. In any case you can't search to big a space recusively or it will fail.

  • Re:All done. (Score:1, Informative)

    by Anonymous Coward on Sunday November 30, 2008 @08:55PM (#25938279)
    fixed for ya.

Nothing succeeds like success. -- Alexandre Dumas

Working...