I implemented a recursive solution for the Knight's tour using Haskell. My algorithm is basically based on Warnsdorff's Rule.
Example:
- Size of chessboard: 5x5
- Starting position: (3,3)
The basic idea based on this example:
(1) Calculate allowed moves from current point (3,3)
=> (1,2),(1,4),(2,1),(2,5),(4,1),(4,5),(5,2),(5,4)
(2) For each of these points, calculate the number of moves which can be performed to reach that point (number of "entrances")
=> ((1,2),2),((1,4),2),((2,1),2),((2,5),2),((4,1),2),((4,5),2),((5,2),2),((5,4),2)
(3) Find the point with the minimum entrances or the first one if all points have the same number of entrances
=> (1,2)
(4) Call same function with the determined point (start recursion)
That way I'm able to determine a way to solve the problem. But now I need to determine all other possible solutions for a starting position (in the example (3,3)). Unfortunately I don't really get how to achieve that.
Ideas are very appreciated.
This is my current Haskell code (providing one solution for a specified starting position):
> kt :: Int -> (Int,Int) -> [(Int,Int)]
> kt dimension startPos = kt' (delete startPos allFields) [startPos] startPos
> where allFields = [(h,v) | h <- [1..dimension], v <- [1..dimension]]
> kt' :: [(Int,Int)] -> [(Int,Int)] -> (Int,Int) -> [(Int,Int)]
> kt' [] moves _ = moves
> kt' freeFields moves currentPos
> | nextField /= (0,0) = kt' (delete nextField freeFields) (moves ++ [nextField]) nextField
> | otherwise = error "Oops ... dead end!"
> where h = fst currentPos
> v = snd currentPos
> nextField = if nextFieldEnv /= [] then fst (head (sortBy sortGT nextFieldEnv)) else (0,0)
> nextFieldEnv = fieldEnv' currentPos freeFields
> sortGT ((a1,a2),a3) ((b1,b2),b3)
> | a3 > b3 = GT
> | a3 < b3 = LT
> | a3 == b3 = EQ
> fieldEnv :: (Int,Int) -> [(Int,Int)] -> [(Int,Int)]
> fieldEnv field freeFields = [nField | nField <- [(hor-2,ver-1),(hor-2,ver+1),(hor-1,ver-2),(hor-1,ver+2),(hor+1,ver-2),(hor+1,ver+2),(hor+2,ver-1),(hor+2,ver+1)], nField `elem` freeFields]
> where hor = fst field
> ver = snd field
> fieldEnv' :: (Int,Int) -> [(Int,Int)] -> [((Int,Int),Int)]
> fieldEnv' field freeFields = [(nField,length (fieldEnv nField freeFields)) | nField <- (fieldEnv field freeFields)]
> where hor = fst field
> ver = snd field