Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Data.List
- import Data.Function
- ----- Knight's Tour Function
- eightDir :: (Int, Int) -> [(Int, Int)]
- eightDir r = [(x -2, y +1),(x -1, y +2),(x -2, y -1),(x -1, y -2),
- (x +2, y -1),(x +1, y -2),(x +2, y +1),(x +1, y +2)]
- where x = fst r
- y = snd r
- knighTour' :: Int -> Int -> [[(Int,Int)]] -> [[(Int,Int)]]
- knighTour' n 1 routes = routes
- knighTour' n size routes = knighTour' n (size-1) (concatMap stepUp routes)
- where stepUp route = [ route ++ [s] | s <- sortedaccess (last route)]
- where sortedaccess r = sortBy (compare `on` accessibility) (filter ( \x -> (x `notElem` route) && (inRegion x) ) (eightDir r))
- inRegion x = (fst x >= 1) && (fst x <= n) && (snd x >= 1) && (snd x <= n)
- accessibility x = length $ filter ( \x -> (x `notElem` route) && (inRegion x) ) (eightDir x)
- knighTour :: Int -> [[(Int,Int)]]
- knighTour n = knighTour' n (n*n) [[(1,1)]]
Advertisement
Add Comment
Please, Sign In to add comment