每日一题 【每日一题】实现汉诺塔-20211130

Jack · 2021年11月30日 · 最后由 Jack 回复于 2021年12月01日 · 93 次阅读
本帖已被设为精华帖!

实现一个汉诺塔游戏:每一步只允许移动一个圆盘,移动时,大圆盘不能在小圆盘上

Jack 将本帖设为了精华贴 11月30日 21:51

参考代码:

class Solution:
    def move(self, n, a, b, c, ans):
        if n == 1:
            ans.append("from" + a + "to" + c)
        else:
            self.move(n-1, a, c, b, ans)
            ans.append("from" + a+"to"+c)
            self.move(n-1, b, a, c, ans)
        return ans

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册