• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

使用python实现递归版汉诺塔示例(汉诺塔递归算法)

PHP admin 12个月前 (05-31) 15次浏览 已收录 0个评论
利用python实现的汉诺塔。带有图形演示

复制代码 代码如下:

from time import sleep

def disp_sym(num, sym):
print(sym*num, end=”)

#recusion
def hanoi(a, b, c, n, tray_num):
if n == 1:
move_tray(a, c)
disp(tray_num)
sleep(0.7)

else:
hanoi(a, c, b, n-1, tray_num)
move_tray(a, c)
disp(tray_num)
sleep(0.7)
hanoi(b, a, c, n-1, tray_num)

def move_tray(a, b):
for i in a:
if i != 0:
for j in b:
if j != 0:
b[b.index(j) – 1] = i
a[a.index(i)] = 0
return
b.append(i)
b.pop(0)
a[a.index(i)] = 0
return

def disp(tray_num):
global a, b, c
for i in range(tray_num):
for j in [‘a’, ‘b’, ‘c’]:
disp_sym(5, ‘ ‘)
eval(‘disp_sym(tray_num – ‘ + j + “[i], ‘ ‘)”)
eval(‘disp_sym(‘ + j + “[i], ‘=’)”)
disp_sym(1, ‘|’)
eval(‘disp_sym(‘ + j + “[i], ‘=’)”)
eval(‘disp_sym(tray_num – ‘ + j + “[i], ‘ ‘)”)

print()

print(‘—————————————————————————‘)

tray_num=int(input(“Please input the number of trays:”))
tray=[]
for i in range(tray_num):
tray.append(i + 1)
a=[0]*tray_num
b=a[:]
c=a[:]

a = tray[:]
disp(tray_num)
hanoi(a, b, c, tray_num, tray_num)


39MO , 版权所有丨如未注明 , 均为原创丨转载请注明原文链接:使用python实现递归版汉诺塔示例(汉诺塔递归算法)
喜欢 (0)
[1353713598@qq.com]
分享 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址