汉诺塔 python

2024-02-15 17:12:33

次只能移动个圆盘。

大圆盘不能放在小圆盘上面。

在Python中用递归的方式来解决这个问题。

```python

def hanoi(n, source, auxiliary, target):

if n > :

# Move n - disks from source to auxiliary, so they are out of the way

hanoi(n - , source, target, auxiliary)

# Move the nth disk from source to target

print('Move disk %i from peg %s to peg %s' % (n, source, target))

# Move the n - disks that we left on auxiliary to target

hanoi(n - , auxiliary, source, target)

```