cis194-solutions icon indicating copy to clipboard operation
cis194-solutions copied to clipboard

Homework1 hanoi4 function has wrong result

Open zhuobinggang opened this issue 7 years ago • 1 comments

hanoi4 :: Integer -> Peg -> Peg -> Peg -> Peg -> [Move]
hanoi4 n src goal tmp1 tmp2
  | n <= 0    = []
  | n == 1    = [(src, goal)]
  {-| n == 2    = [(src, tmp1), (src, goal), (tmp1, goal)]-}
  | n == 3    = [(src, tmp1), (src, tmp2), (src, goal), (tmp2, goal), (tmp1, goal)]
  | otherwise = hanoi4 (n-1) src tmp1 goal tmp2 ++ hanoi4 1 src goal tmp1 tmp2
                ++ hanoi4 (n-1) tmp1 goal src tmp2

I try to load it and run length(hanoi4 15 "a" "b" "c" "d"),the result is 24575,but expected result is 129

zhuobinggang avatar Jan 22 '18 05:01 zhuobinggang

His implementation solves the problem but does not minimize the number of moves. If you want to get the expect result you have to implement this algorithm https://en.wikipedia.org/wiki/Tower_of_Hanoi#Frame–Stewart_algorithm.

dmanc avatar Dec 30 '18 06:12 dmanc