Knight's Tour

The problem:

Can a knight surf all the squares of a chessboard exactly by reaching one square for only once?

It's said that this problem was first proposed by Euler and then interested Gauss much. Well, we aint mathematicians. We just code.

Solution:

Warnsdorff's rule

This problem is in fact a special case of the Hamiltonian Path problem. A _plain_ backtracking is apparently not acceptable, as it would grow exponently. We need a _heuristic_ algorithm. Luckily (for us), Mr. Warnsdorff proposed one back in 1823.

The basic rule is that, at each step, we gotta move to the position from which we have fewest choices to move to other places. Sounds a little bit confusing? Might feel better by just taking a look at the code. It inputs a starting position and outputs one possible route.

If you do the math, for a 8 x 8 board, a knight can finish his tour from any starting square. Test my code for this. Here is the output.

The most interesting part is maybe how to break the ties of the Warnsdorff rule. By carefully choosing a 'smart' order of directions, we can avoid any extra effort (too lucky!). But to get real, we maybe need to apply randomized algorithms and/or utilize the symmetric characteristics of the chessboard.

Leave a comment

Recent Entries

  • Running Spaz on WebOS Emulator

    > You got root, you got everything. Yeah, Palm Pre is hot these days. Now with the [webos emulator](http://www.geektang.com/2009/06/linuxwebos.html) and [ssh](http://www.geektang.com/2009/06/sshwebos.html) available, you can have...

  • Haskell Platform

    >The Haskell Platform is a blessed library and tool suite for Haskell distilled from Hackage, along with installers for a wide variety of systems. IOW,...

  • When MT Meets iPhone/iPod Touch

    Just found [iMT](http://plugins.movabletype.org/imt/). It's the iphone / ipod touch interface for mt4. So now you can add/edit blog entries / comments on your favorite device...

  • All Project Euler Problems Solved

    [Project Euler](http://projecteuler.net) continues to be fun. First reached 100% when there were 240 problems. And have successfully guarded that honor since =). (As of this...

  • Million Digits of E, Sqrt(2), Pi

    > More puzzles, more fun. Besides [acm oj](http://acm.tju.edu.cn/toj/ranklist.html) and [projecteuler](http://projecteuler.net), i also like to solve the puzzles on [spoj](http://spoj.pl). The ACM/ICPC rules are rather limited...

Close