temp
= int(input('输入汉诺伊塔的层数:'))
def hanoi(temp
, x
, y
, z
):
step
= 0
if temp
== 1:
print(x
, '→', z
)
step
+= 1
else:
hanoi
(temp
-1, x
, z
, y
)
step
+= 1
print(x
, '→', z
)
step
+= 1
hanoi
(temp
-1, y
, x
, z
)
step
+= 1
print('你移动了', step
, '步')
hanoi
(temp
, 'X', 'Y', 'Z')
*这里有点问题,就是输出的移动步数不是输出的最后移动的总步数,还需要在后续进行改进。
转载请注明原文地址:https://ipadbbs.8miu.com/read-62203.html