def hanoi_recur(n,reverse=True): if n==1: return 1,1 possible=[] iter_range=range(n-1,0,-1) if reverse else range(1,n) for i in iter_range: _, min_v = hanoi_recur(n-i,reverse) possible.append((i,2*min_v+2**i-1)) return min(possible,key=lambda x:x[1])
如上所示,
hanoi_recur(10000,reverse=False)直接栈溢出
hanoi_recur(10000,reverse=True)不会.
感觉比较神奇.