python基本语法2.10--hanoi问题

问题描述:利用用中间的柱子把最左边的柱子a上的圆盘移到最右边的柱子c,借助中间柱子b

思路:采用递归

   首先,将n-1个柱子,从a移到b

   然后,将最下面的一个柱子从a移动到c

   最后,将n-1个柱子,从b移动到c

代码:

def move(n,source,target,helper):
    if(n==1):
        print(source,"->",target)
        return
    move(n-1,source,helper,target)
    move(1,source,target,helper)
    move(n-1,helper,target,source)
move(3,"a","c","b")

结果:

a -> c
a -> b
c -> b
a -> c
b -> a
b -> c
a -> c

 

posted on 2017-10-20 09:39  小嘤嘤  阅读(286)  评论(0编辑  收藏  举报

导航