汉诺塔

递归经典题目

def hanoi(num,_from,to,others):
    if num == 1:
        move(num,_from,to)
    else:
        hanoi(num-1,_from,others,to)
        move(num,_from,to)
        hanoi(num-1,others,to,_from)


def move(object,_from,to):
    print('move {object} from {_from} to {to}'.format(object=object,_from=_from,to=to))


def main():
    hanoi(3,'a','c','b')


if __name__ == '__main__':
    main()

你可能感兴趣的:(汉诺塔)