『壹』 python請用遞歸演算法編程解決漢諾塔問題 在線等
這是Python3系統自帶的一個例子,估計就是這個意思,本來他是6個盤子,按照你要求改成4個了。遞歸演算法沒問題,描述也非常詳細 ;)
#!/usr/bin/envpython3
fromturtleimport*
classDisc(Turtle):
def__init__(self,n):
Turtle.__init__(self,shape="square",visible=False)
self.pu()
self.shapesize(1.5,n*1.5,2)#square-->rectangle
self.fillcolor(n/6.,0,1-n/6.)
self.st()
classTower(list):
"Hanoitower,asubclassofbuilt-intypelist"
def__init__(self,x):
"createanemptytower.xisx-positionofpeg"
self.x=x
defpush(self,d):
d.setx(self.x)
d.sety(-150+34*len(self))
self.append(d)
defpop(self):
d=list.pop(self)
d.sety(150)
returnd
defhanoi(n,from_,with_,to_):
ifn>0:
hanoi(n-1,from_,to_,with_)
to_.push(from_.pop())
hanoi(n-1,with_,from_,to_)
defplay():
onkey(None,"space")
clear()
try:
hanoi(6,t1,t2,t3)
write("pressSTOPbuttontoexit",
align="center",font=("Courier",16,"bold"))
exceptTerminator:
pass#turtledemouserpressedSTOP
defmain():
globalt1,t2,t3
ht();penup();goto(0,-225)#writerturtle
t1=Tower(-250)
t2=Tower(0)
t3=Tower(250)
#maketowerof6discs
foriinrange(4,0,-1):
t1.push(Disc(i))
#preparespartanicuserinterface;-)
write("pressspacebartostartgame",
align="center",font=("Courier",16,"bold"))
onkey(play,"space")
listen()
return"EVENTLOOP"
if__name__=="__main__":
msg=main()
print(msg)
mainloop()
『貳』 python 遞歸漢諾塔求解代碼問題
報錯意思是變數n沒有被定義。
你把層數傳給hanoi了。
把hanoi=(int(input('請輸入漢諾塔層數')))里的hanoi改為n就行了,其他不用改
『叄』 python利用遞歸解決漢諾塔問題,求大神解釋一下代碼
這是一個典型的遞歸程序
當只有一層的時候,直接把x放到z上結束
當大於1層的時候,先把x和z放到y上,然後繼續遞歸
把y放到x上,然後放到z上,結束處理
『肆』 【python】漢諾塔遞歸
系統自帶的演示代碼,可以研究一下
#!/usr/bin/envpython3
"""turtle-example-suite:
tdemo_minimal_hanoi.py
Aminimal'TowersofHanoi'animation:
lefttotherightpeg.
Animhoquiteelegantandconcise
,which
isderivedfromthebuilt-intypelist.
Discsareturtleswithshape"square",but
()
---------------------------------------
ToexitpressSTOPbutton
---------------------------------------
"""
fromturtleimport*
classDisc(Turtle):
def__init__(self,n):
Turtle.__init__(self,shape="square",visible=False)
self.pu()
self.shapesize(1.5,n*1.5,2)#square-->rectangle
self.fillcolor(n/6.,0,1-n/6.)
self.st()
classTower(list):
"Hanoitower,asubclassofbuilt-intypelist"
def__init__(self,x):
"createanemptytower.xisx-positionofpeg"
self.x=x
defpush(self,d):
d.setx(self.x)
d.sety(-150+34*len(self))
self.append(d)
defpop(self):
d=list.pop(self)
d.sety(150)
returnd
defhanoi(n,from_,with_,to_):
ifn>0:
hanoi(n-1,from_,to_,with_)
to_.push(from_.pop())
hanoi(n-1,with_,from_,to_)
defplay():
onkey(None,"space")
clear()
try:
hanoi(6,t1,t2,t3)
write("pressSTOPbuttontoexit",
align="center",font=("Courier",16,"bold"))
exceptTerminator:
pass#turtledemouserpressedSTOP
defmain():
globalt1,t2,t3
ht();penup();goto(0,-225)#writerturtle
t1=Tower(-250)
t2=Tower(0)
t3=Tower(250)
#maketowerof6discs
foriinrange(6,0,-1):
t1.push(Disc(i))
#preparespartanicuserinterface;-)
write("pressspacebartostartgame",
align="center",font=("Courier",16,"bold"))
onkey(play,"space")
listen()
return"EVENTLOOP"
if__name__=="__main__":
msg=main()
print(msg)
mainloop()
『伍』 哪位大佬有python漢諾塔的教程
學到遞歸的時候有個漢諾塔的練習,漢諾塔應該是學習計算機遞歸演算法的經典入門案例了,所以本人覺得可以寫篇博客來表達一下自己的見解。這markdown編輯器還不怎麼會用,可能寫的有點格式有點丑啦,各位看官多多見諒.
網上找了一張漢諾塔的圖片,漢諾塔就是利用用中間的柱子把最左邊的柱子上的圓盤依次從大到小疊上去,說白了就是c要跟原來的a一樣
童鞋們理解了漢諾塔的遞歸演算法原理後,可以寫個程序來試試,這里只是學到Python的遞歸所以用了Python,童鞋們可以用其他語言實現,漢諾塔確實能幫助理解遞歸原理,遞歸在程序設計中的重要性不言而喻啦!
『陸』 求python大神幫忙解釋一下 這個漢諾塔程序的步驟
def my_print(args):
print args
def move(n, a, b, c):
my_print ((a, '-->', c)) if n==1 else (move(n-1,a,c,b) or move(1,a,b,c) or move(n-1,b,a,c))
注釋:漢諾塔模型輸入move (n, 'a', 'b', 'c')
例如n=3
move(2,a,c,b)自循環
move(1,a,b,c)
move(2,b,a,c) 自循環
循環完畢,輸出
你這段代碼也是類似自循環
『柒』 關於python遞歸函數實現漢諾塔
仔細看一下 5-7行調用 move 時候的參數順序, 不是你說的那樣沒有變:
#5 的含義是將 A 上的前 n-1 個移動到 B
#6 : 將 A 最後一個移動到 C
#7: 將 B 上的 n-1 (即#5 從 A 移動過來的 n-1) 個移動到 C
『捌』 python解決漢諾塔問題
解漢諾塔最簡單的做法就是遞歸:
類似如何將大象裝進冰箱:1)將冰箱門打開;2)把大大象放進去;3)把冰箱門關上……
我們將所有的盤都在同一個桿上從大到小排列視為【完美狀態】,那麼,目標就是將最大碟片為n的完美狀態從a桿移到b桿,套用裝大象的思路,這個問題同樣是三步:
1)把n-1的完美狀態移到另一個桿上;
2)把n移到目標桿上;
3)把n-1的完美狀態移到目標桿上。
如下: