⑴ 求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】漢諾塔遞歸
系統自帶的演示代碼,可以研究一下
#!/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漢諾塔問題,結果怎麼會出線移動過程
你居然在這兒也發帖,為了賺財富值我也是拼了,
print 'a', '-->', 'c', 你改成這樣,你會發現列印出來的都是一樣,因為這裡面 a c都是字元
print a, '-->', c ,這兒的 a,c是函數傳入的變數, 在函數運行的過程中, a b c指向的變數是不同的
move(n - 1, a, c, b)
move(n - 1, b, a, c)
我在代碼中加了一點注釋,希望有用, 你要是實在看不明白很正常,遞歸函數需要一點時間理解
http://www.liaoxuefeng.com/wiki//
⑷ 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解決漢諾塔問題
解漢諾塔最簡單的做法就是遞歸:
類似如何將大象裝進冰箱:1)將冰箱門打開;2)把大大象放進去;3)把冰箱門關上……
我們將所有的盤都在同一個桿上從大到小排列視為【完美狀態】,那麼,目標就是將最大碟片為n的完美狀態從a桿移到b桿,套用裝大象的思路,這個問題同樣是三步:
1)把n-1的完美狀態移到另一個桿上;
2)把n移到目標桿上;
3)把n-1的完美狀態移到目標桿上。
如下:
⑺ 漢諾塔python
Solves the Towers of Hanoi problem on n discs. The discs are labeled
* in increasing order of size from 1 to n and the poles are labeled
* A, B, and C.
*
* % java Hanoi 3
* Move disc 1 from A to C
* Move disc 2 from A to B
* Move disc 1 from C to B
* Move disc 3 from A to C
* Move disc 1 from B to A
* Move disc 2 from B to C
* Move disc 1 from A to C
以上為模擬結果,從結果中找遞歸規律,你的疑點也能得到解決
⑻ python漢諾塔非遞歸
python漢諾塔非遞歸,運用list和function知識的解答
無論stack還是recursion都是從漢諾塔的原理去解決問題,但如果已經想清楚漢諾塔的原理,其實只用把答案print出來就行了
先找規律:
一層:A-->C
兩層:A-->B
-------
A-->C
-------
B-->C
三層:A-->C
A-->B
C-->B
-------
A-->C
-------
B-->A
B-->C
A-->C
注意到n層漢諾塔有(2**n) - 1 個步驟,而中間的一步(兩個分割線之間)都是「A-->C」,中間的這一步將這一層漢諾塔的解分為上下兩個部分
仔細觀察,上面一部分是將上一層的解中所有的B,C交換,下面一部分是將上一層的解中所有的A,B交換
例如第二層是:
A-->B
A-->C
B-->C
第三層上部分就將第二層的解的C換成B,B換成C,即得出:
A-->C
A-->B
C-->B
第三層下部分就將第二層的解的A換成B,B換成A,即得出:
B-->A
A-->C
C-->B
這個規律同樣適用於第一層,和以後的所有層
然後就好辦了,代碼如圖:
代碼
其中convertAB,convertBC就是AB交換,BC交換的函數,這兩個函數可以自己定義,用中間變數即可
⑼ python 漢諾塔問題 如圖,為什麼列印完 A→B 時n還是等於1
3,4,5在遞歸的層級上都是在2下的,它們3個是同級,它們使用的實參都是2傳給它們的。所以都用的同一個實參變數n,所有n-1都是1。