漢諾塔 python
2024-02-15 17:12:33
次只能移動(dòng)個(gè)圓盤(pán)。
大圓盤(pán)不能放在小圓盤(pán)上面。
在Python中用遞歸的方式來(lái)解決這個(gè)問(wèn)題。
```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)
```