汉诺塔 递归算法

来源:互联网 发布:java se8.0 编辑:程序博客网 时间:2024/06/14 05:17
count = 0def hanoi(n,A,B,C):    '''    算法①先将n-1个碟子借助C,从A移到B;        ②将第n个碟子从A移到C;        ③将n-1个碟子借助A,从B移到C;    '''    global count        '''    if n = 1 :        print("Move ", str(n)," from " ,A ," to ",C )    '''         if n ==0 :        return     else :        hanoi(n-1,A,C,B)        print("Move ", str(n)," from " ,A ," to ",C )        count+=1        hanoi(n-1,B,A,C)#测试hanoi(4,"Left","Mid","Right")print(count)

这里写图片描述