首页 > 代码库 > Python - 汉诺塔

Python - 汉诺塔

def hanoi(n, a, b, c):
	if(n == 1):
		print(a, ‘-->‘, c)
		return
	hanoi(n - 1, a, c, b)
	print(a, ‘-->‘, c)
	hanoi(n - 1, b, a, c)

# Output of hanoi(3, ‘A‘, ‘B‘, ‘C‘):
# A --> C
# A --> B
# C --> B
# A --> C
# B --> A
# B --> C
# A --> C

Python - 汉诺塔