Follow Slashdot blog updates by subscribing to our blog RSS feed


Forgot your password?
Programming Math IT Technology Entertainment Games

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

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:
  • awesome (Score:5, Funny)

    by sofar ( 317980 ) on Sunday November 30, 2008 @03:03PM (#25935075) Homepage

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

    • Re: (Score:2, Informative)

      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 )

      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.

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

      No need for a next python version. Just let the code grow and in any mo
      Traceback (most recent call last):
          File "", line 1, in
      TypeError: 'int' object is not callable

  • Easy (Score:5, Funny)

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

    C-x C-m KnightsPuzzle

  • All done. (Score:3, Interesting)

    by DeadDecoy ( 877617 ) on Sunday November 30, 2008 @03:06PM (#25935131)
    There. I did it in one line of code.
    #!/usr/bin/env python import sys g_sqSize = -1 # the board size, passed at runtime g_board = [] # the board will be constructed as a list of lists def main(): global g_sqSize if len(sys.argv) != 2: g_sqSize = 8 # Default: Fill the normal 8x8 chess board else: try: g_sqSize = int(sys.argv[1]) # or, the NxN the user wants except: print "Usage: " + sys.argv[0] + " " sys.exit(1) for i in xrange(0, g_sqSize): g_board.append(g_sqSize*[0]) # Fill the board with zeroes Fill(0,0,1) # Start the recursion with a 1 in the upper left print "No solution found" # if the recursion returns, it failed def InRangeAndEmpty(ty,tx): # check if coordinates are within the board return ty>=0 and tx>=0 and ty
  • by Anonymous Coward on Sunday November 30, 2008 @03:07PM (#25935145)

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

  • by gardyloo ( 512791 ) on Sunday November 30, 2008 @03:09PM (#25935161)
    • Good grief! That has to be the most unreadable blob of code I've ever seen...

      Here's a taste of a relatively readable part:


      KnightTour[rows_Integer, columns_Integer, start_List, end_List:{}, \
      HidePaths_Integer:0] :=
      Module[{sR = rows+1, sC = columns+1, i = 0, j = 0, path, endMoves, tree = \
      {0}, SNew, KnightMoves, FeasibleMoves, area},

      path = If[IntegerQ[start[[1]]], {start}, start];

      • Re: (Score:3, Interesting)

        by gardyloo ( 512791 )

        That's because you're looking at some MathML code. What one actually types into Mathematica, and sees in Mathematica (or sees in a raw text file version IF the "InputForm" of the code is looked at) is the following. Unfortunately, the code ends suddenly because slashcode somehow doesn't allow more to be shown. BAAAAAD slashcode.

        Complaining about the readability of what you posted is like complaining about the raw HTML which goes into this webpage.

        KnightTour[rows_Integer, columns_Intege

  • Try lisp (Score:4, Insightful)

    by FlyingBishop ( 1293238 ) on Sunday November 30, 2008 @03:09PM (#25935171)

    Advantages that have nothing to do with libraries, and can be traced back to the combination of (a) functional programming and (b) the perfect syntax that Python offers. I would truly be amazed to see anyone writing the same logic in C++ in anything less than 3 times the lines of code I wrote in Python.

    Though I have better things to do than actually try, looking over the code FTFA, I have to say that I think a transliteration of this code into Scheme or Lisp would actually look cleaner than Python. And I do know that that would deal with the problem the writer ran into, namely that Python has an absurdly low recursion limit.

    I do like Python's syntax (for anything under 100 lines of code) but calling it a model of functional programming is just silly.

    • Re: (Score:2, Interesting)

      by maxume ( 22995 )
      His code:

      # recurse using our neighbours, trying first the ones with the
      # least amount of free neighbours, i.e. the "loners"
      for ty,tx in sorted(emptyNeighbours, key=lambda c: reduce(
      lambda x,y: x+y,
      map(lambda j: InRangeAndEmpty(c[0]+j[0], c[1]+j[1]) and 1 or 0,

      Idiomatic python(or at least more-so):

      def sort_key(c):
  • 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: []

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

    • Re:better algo (Score:5, Interesting)

      by eulernet ( 1132389 ) on Sunday November 30, 2008 @05:53PM (#25936723)

      The ultimate algorithm is called Warnsdorf's heuristic: []
      It solves all possible orders (>100x100) in less than a second.

      The algorithm cited in the article is really shitty, because it requires recursion.

      Hint: I implemented an algorithm to enumerate all magic knight tours (magic, like in magic squares): []

      • Re: (Score:3, Interesting)

        by misterpib ( 924404 )
        A non-recursive Python version which uses Warnsdorf's heuristic: []

        It's faster than the one in TFA as well, though it has no backtracking, so it won't find some solutions once you get bigger than 76x76, but at least it doesn't overflow the stack.

        It also will tell you whether it found an open, closed, or incomplete path.
      • The algorithm cited in the article is really shitty, because it requires recursion.

        Wait a second. What's wrong with recursion?

        • by seann ( 307009 ) <> on Sunday November 30, 2008 @07:36PM (#25937625) Homepage Journal

          Wait a second. What's wrong with recursion?

          • 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.

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

      • Recursion is not always bad. It often makes code much easier to understand, and a good compiler can sometimes optimize the recursion away in the object code.
  • by Anonymous Coward on Sunday November 30, 2008 @03:17PM (#25935257)

    wrapper(Size, [X, Y], Path) :-
            X == 1,
            Y == 1,
            Depth is Size * Size - 1,
            worker(Size, [X, Y], Depth, [], ReversedPath),
            reverse(ReversedPath, Path),
            write(Path), nl.
    worker(_, State, 0, CurrentPath, [State|CurrentPath]).
    worker(Size, State, Depth, CurrentPath, FinalPath) :-
            DepthM1 is Depth - 1,
            move_generator(Size, State, NewState),
            not(checker(NewState, CurrentPath)),
            worker(Size, NewState, DepthM1, [State|CurrentPath], FinalPath).
    checker(State, [State|_]).
    checker(State, [_|StateList]) :-
            checker(State, StateList).
    move_generator(Size, [X, Y], [NewX, NewY]) :-
            move(MoveX, MoveY),
            NewX is X + MoveX, NewX == 1,
            NewY is Y + MoveY, NewY == 1.
    move(1, 2).
    move(2, 1).
    move(2, -1).
    move(1, -2).
    move(-1, -2).
    move(-2, -1).
    move(-2, 1).
    move(-1, 2).

    • by phantomfive ( 622387 ) on Sunday November 30, 2008 @04:35PM (#25936027) Journal
      Yeah, and here's one in Java [] that does the same thing but with an animated GUI (with only 10 more lines of code!). Thus the claim from the article is a bit much:

      I would truly be amazed to see anyone writing the same logic in C++ in anything less than 3 times the lines of code I wrote in Python. And even if this is somehow possible (using external libraries like BOOST, I'd wager), the code will take longer to write, and it will be far more difficult to comprehend or refactor...

      And I'd wager that this guy has never worked on huge projects. Any chunk of code that is less than a hundred lines is not going to be difficult to refactor; in fact, such a short piece of code probably gets longer and more confusing by adding object oriented structure (notice his code isn't encapsulated into a class or anything). The real advantages of structured programming isn't seen until you have a large project that has constantly changing requirements. That is where flexibility REALLY makes a difference.

      I would also argue that any modern language gives you everything you need to write good, flexible code, and the quality of the code produced is more closely related to the skill of the programmer, than it is to the programming language itself.

      In fact, for myself, it would not be an exaggeration to say I can write more flexible code in assembly now than I could five years ago in any language. Of course, it would be well structured assembly, not the wild mess of code I've written in previous years. YMMV.

      • by IamTheRealMike ( 537420 ) on Sunday November 30, 2008 @06:35PM (#25937095)

        Yeah, that sort of assertion bugs me. My own experience has been the exact opposite - attempting to understand large Python programs that have evolved over a number of years is damn near impossible. I know, I've tried. The terseness of the language and the absolute lack of explicit typing means you can't just open up a random function and understand what's going on. You often have to trace backwards through the code just to discover what it's attempting to do.

        Typically Python programmers try and paper over this problem with tons of doc comments. Problem is that like any comment, they can get out of date, and often aren't useful anyway. If I had a dollar for every time I've seen:

        foo: The foo to bar.

        in a Python doc comment, I'd be a rich guy. What is a foo exactly? A class? A tuple? A list of tuples of classes? Or worse, any of the above?

        In contrast, I've found it very easy to dive right into some of the large C++ code bases we have at work and immediately understand what the code does and how it does it, largely because C++ is more explicit and the (partly redundant) specification of type information means you can rapidly find how different components interact. Redundant comments are kept to a minimum. Comprehension is radically improved.

        This is very useful when attempting to understand error messages, for instance. My absolute worst nightmare troubleshooting wise is running a giant Python script and getting a type error 20 frames deep, because I know it could easily burn an afternoon just untangling the mess. More explicit languages rarely seem to have this problem.

        • by Tack ( 4642 ) on Sunday November 30, 2008 @11:15PM (#25939387) Homepage

          in a Python doc comment, I'd be a rich guy. What is a foo exactly? A class? A tuple? A list of tuples of classes? Or worse, any of the above?

          This is certainly one of the practical drawbacks of duck-typing. But name-based polymorphism is exceedingly powerful, and with great power comes great responsibility. (Namely, to document one's arguments and return values properly.)

      • Re: (Score:3, Insightful)

        That Java code is only 10 lines longer, but it doesn't include the code for some other classes it uses to solve the problem.

        But anyhow, you're missing his point. The basic backtracking algorithm for the problem is simple in any language, and could indeed be made much shorter in Java (w/o the entire search framework). He's talking about improving the search with a heuristic (in his case, what is known as a "minimum remaining values" search heuristic among the AI folks). But he's still probably wrong, as you'

      • Re: (Score:3, Informative)

        by setagllib ( 753300 )

        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.

    • by RedWizzard ( 192002 ) on Sunday November 30, 2008 @06:02PM (#25936777)
      It can be done concisely in functional languages, e.g. Haskell:

      knights :: Int -> [[(Int,Int)]]
      knights n = loop (n*n) [[(1,1)]]
              where loop 1 = map reverse . id
                      loop i = loop (i-1) . concatMap nextMoves

                      nextMoves already@(x:xs) = [next:already | next <- possible]
                              where possible = filter (\x -> on_board x && not (x `elem` already)) $ jumps x

                      jumps (x,y) = [(x+a, y+b) | (a,b) <- [(1,2), (2,1), (2,-1), (1,-2), (-1,-2), (-2,-1), (-2,1), (-1,2)]]
                      on_board (x,y) = (x >= 1) && (x <= n) && (y >= 1) && (y <= n)

  • Perl (Score:5, Interesting)

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

    use Chess;

    $knight = Chess::Piece::Knight->new();
    $board = Chess::Board->new(100, 100, setup => {
                    $knight => "a1";


    • 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 [].

      Perl is awesome!

      • What does the quality of the language have to do about a sister projected of keeping and storing 3rd party libraries. I actually hate it because of that because you get these perl apps and they assume that you have some stupid library installed and you need to keep on trying over and over again for the right library.

        • Because it saves time writing code and "standardizes" common functinality (like a Chess board abstraction). Both of those are indirect ways of saying CPAN makes complicated problems easier to solve. The whole point of writing software is to solve problems, and CPAN makes it easier. Why reinvent the wheel, even if it is only 60 lines of Python?

          Also: CPAN can automatically grab dependencies, so I'm not really sure what you're whining about in your second sentence.

        • ... and you need to keep on trying over and over again for the right library.

          Or just read the requirements of the software you're installing. Maybe that would be too easy?

      • Re: (Score:2, Informative)

        by Anonymous Coward

        (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. ;)

    • It's a pretty poor language, if if makes its users confuse code that calls external modules with actual implementations.

  • dump the recursion (Score:5, Interesting)

    by Jecel Assumpcao Jr ( 5602 ) on Sunday November 30, 2008 @03:30PM (#25935413) Homepage

    With the "added intelligence" of the second version, the recursive search devolved into a linear one since the very first attempt at each step will lead to a good solution (add a print to the backtracking part and see if this isn't the case).

    So you might as well convert the recursion into a loop and eliminate the stack overflows for large boards.

  • by berend botje ( 1401731 ) on Sunday November 30, 2008 @03:35PM (#25935473)
    Is submitter really thinking he is special because he implemented a trivial backtracking algorithm that every first semester CS student has done?
    • by jellomizer ( 103300 ) on Sunday November 30, 2008 @04:14PM (#25935853)

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

    • It isn't anything special in particular. What _is_ special is doing it without receiving the instruction first.

      I think reinventing the wheel is a good thing in some situations; one is when you reinvent the wheel to study it but not use it. Another is when you reinvent the wheel without having been shown what a wheel is and how it works.

      It's a sign that you can think for yourself and come up with good solutions to the problems you want to solve independent of instruction.

      • by jellomizer ( 103300 ) on Sunday November 30, 2008 @05:48PM (#25936691)

        Yes, but a lot of this stuff really isn't worth posting online. Espectially Slashdot I have created many algorithms myself without the need to post it for slashdot acceptance. Some interesting compression algorithms, Memory management algorithms... Whatever that I feel like exploring today. But it is for my own personal knowledge not for public viewing of my code as my method will be to prove some particular point to myself nor will it be efficient or complete, and any attempt to have it posted like the guy who posted this thread will just get critized for anything that is not the best as it could possibly be.

    • I did this in high school you insensitive clod!

      Oh and it was in FORTRAN :P

  • OMG (Score:4, Funny)

    by l3v1 ( 787564 ) on Sunday November 30, 2008 @03:46PM (#25935569)
    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.
  • by shking ( 125052 ) <babulicm&cuug,ab,ca> on Sunday November 30, 2008 @04:13PM (#25935845) Homepage
    Here's a solution in 14 lines of APL []. I'm pretty sure they could've made it shorter, but readability would've been even worse. APL has been called a "write-only language".
  • by Speare ( 84249 ) on Sunday November 30, 2008 @04:21PM (#25935931) Homepage Journal
    I know it's a joke to refer to "obfuscated Perl" but this was my one attempt at doing something silly with it. []
    • It finds solutions to the 6x10 pentominoes board (exhaustively)
    • To find places that pieces will fit, it employs regular expressions
    • To draw pieces into the board, it employs an embedded tape-driven LOGO-like turtle language
    • It prints solutions as a specially formatted quine of its own source code
    • Any printed solution can be run separately
    • It takes hours and hours to find solutions
  • by gillbates ( 106458 ) on Sunday November 30, 2008 @04:25PM (#25935949) Homepage Journal

    As part of my undergrad education. Taking less than a second on today's hardware is nothing spectacular; the secret is in the algorithm: You rate the squares according to the number of moves available from that square and, when given a choice, pick the square with the least number of moves. This way, you don't work yourself into a dead-end situation as frequently. Combine this with a little backtracking, and you've got a nice example to show how algorithm selection has a much larger impact on runtime performance than language selection.

    Incidentally, 200 MHz was considered a fast CPU when I did it, and I remember it taking 8 billion moves and all night without finding a solution. Until, that is, we implemented the preferential choice part of the algorithm. After that, it was pretty much instantaneous.

  • I love python, I use it all the time now, but back in high school, for AP Computer Science (I was the first class to take the Java AP test), we had to implement a recursive algorithm in Java to do it. It was fast, and could solve the problem for whatever size board we wanted. We found that a few board sizes were impossible.

    Anyhow, what's the point? I didn't think this was a hard problem at all when we did it.
  • by Anonymous Coward

    Not cool.

  • by Vexorian ( 959249 ) on Sunday November 30, 2008 @06:32PM (#25937065)
    I take the point of the blog plug was that I shouldn't be able to do it in C++ with 60 lines....

         1    #include <set>
         2    #include <iostream>
         3    #include <cassert>
         4    using namespace std;
         6    int dx[8]={1,1,-1,-1,2,2,-2,-2}, dy[8]={2,-2,2,-2,1,-1,1,-1};
         7    int D[50][50];
         8    int N,C;
        10    #define valid(x,y) ((x>=0) && (x<N) && (y>=0) && (y<N) && (D[x][y]==-1 ) )
        12    bool show()
        13    {
        14        for (int i=N;i--;)
        15        {
        16            for (int j=N;j--;)
        17                cout<<"\t"<<D[i][j];
        18            cout<<"\n";
        19        }
        20        return true;
        21    }
        23    bool rec(int x, int y)
        24    {
        25        D[x][y]=C++;
        26        if(C==N*N)
        27            return show();
        29        set< pair<int, pair<int,int> > > poss;
        30        for (int r=8;r--;)
        31            if(valid(x+dx[r], y+dy[r]))
        32            {
        33                int neighb=0;
        34                for (int t=8;t--;)
        35                    neighb+= valid(x+dx[r]+dx[t],y+dy[r]+dy[t] );
        36                poss.insert( make_pair(neighb, make_pair(x+dx[r],y+dy[r] ) ));
        37            }
        39        for (typeof(poss.begin()) q=poss.begin(); q!=poss.end(); q++) //hence the reason I am waiting for c++0x
        40            if (rec(q->second.first, q->second.second))
        41                return true;
        43        D[x][y]=-1;
        44        C--;
        46        return false;
        47    }
        49    void solve(int n)
        50    {
        51        N=n, C=0;
        52        memset(D,-1,sizeof(D));
        53        assert(rec(0,0)) ;
        54    }
        56    int main()
        57    {
        58        int n;
        59        while((cin>>n) && (n>0))
        60            solve(n);
        61        return 0;
        62    }

    The bastards! Those darn brackets force me to have 2 extra lines :(
    • Just change your coding style to have the { on the same line as if and for, which is a popular style, and you've done it.

  • TFA has basically stumbled upon Warnsdorff's algorithm []. It's a great method, but it doesn't work for really large boards due to blind alleys. There's another method available which uses decomposition to achieve a linear running time(in # of squares), but which is quite a bit more complex to implement. There's a nice tweak to the algorithm which can get much farther than the unmodified original: in the event of a tie (where two candidates have the same number of open neighbours), break the tie by choosing th

  • Memo to OP: Never, never, brag how short your code is, especially if you're using something as bloated as Python.

    There's always a more concise way. And a faster way.

    You're just asking for a beating, and well-deserved.

  • > Try beating this, fellow coders!

    Here it is in 30 lines of Water. Oh, and it outputs in HTML.

    <class knights_tour size=8 board=opt >
    <method make> .<set board=.size.<for_each combiner=insert> .size.<for_each combiner=insert> false </for_each> </for_each> /> .<fill/>
    <method in_range_and_empty x=req y=req>
    <and .board.<has y/
  • by saddino ( 183491 ) on Sunday November 30, 2008 @09:48PM (#25938733)

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

  • Batteries included (Score:4, Interesting)

    by XNormal ( 8617 ) on Monday December 01, 2008 @05:03AM (#25941159) Homepage

    There is an elegant Knight's Tour solver right inside your Python distribution. You can find it at /usr/lib/python2.5/test/ []. Written by Tim Peters (a.k.a. timbot).

"I shall expect a chemical cure for psychopathic behavior by 10 A.M. tomorrow, or I'll have your guts for spaghetti." -- a comic panel by Cotham