fork download
  1. type Point = (Int, Int)
  2. type Pair = (Point, Point)
  3. type Node = [Pair]
  4.  
  5. main :: IO ()
  6. main = do
  7. print $ length $ solve 8
  8.  
  9. plus :: Point -> Point -> Point
  10. plus (x1, y1) (x2, y2) = (x1+x2, y1+y2)
  11.  
  12. minus :: Point -> Point -> Point
  13. minus (x1, y1) (x2, y2) = (x1-x2, y1-y2)
  14.  
  15. move :: Pair -> Point -> Pair
  16. move (f, b) p = (f `plus` p, b `minus` p)
  17.  
  18. moves :: Pair -> Pair -> [Pair]
  19. moves (pf, pb) (qf, qb) = map (move (pf, pb)) [f, r, l]
  20. where
  21. (dx, dy) = pf `minus` qf
  22. f = (dx, dy)
  23. r = (-dy, dx)
  24. l = (dy, -dx)
  25.  
  26. nexts :: Node -> [Node]
  27. nexts (p1:p0:ps) = [p2:p1:p0:ps | p2 <- moves p1 p0]
  28.  
  29. exists :: Point -> [Pair] -> Bool
  30. exists p = any (either p)
  31. where
  32. either p (pf, pb) = p == pf || p == pb
  33.  
  34. solve :: Int -> [Node]
  35. solve size = solutions initial
  36. where
  37. center :: Point
  38. center = (size `div` 2, size `div` 2)
  39.  
  40. p0 :: Pair
  41. p0 = (center, center)
  42.  
  43. initial :: Node
  44. initial = [move p0 (0, 1), p0]
  45.  
  46. atboundary :: Point -> Bool
  47. atboundary (x, y) = (x == 0) || (x == size) || (y == 0) || (y == size)
  48.  
  49. solutions :: Node -> [Node]
  50. solutions n
  51. | atboundary f = [n]
  52. | exists f ps = []
  53. | otherwise = [n'' | n' <- nexts n,
  54. n'' <- solutions n']
  55. where
  56. (f,b):ps = n
  57.  
Success #stdin #stdout 0.65s 4612KB
stdin
Standard input is empty
stdout
184525