本文目錄一覽:
- 1、python利用遞歸解決漢諾塔問題,求大神解釋一下代碼
- 2、python解決漢諾塔問題?
- 3、標題:用Python編碼描述漢諾塔步驟
- 4、【python】漢諾塔遞歸
- 5、Python 中關於漢諾塔的問題,這個程序,給定參數n的數值,怎麼在程序里運行
- 6、求python大神幫忙解釋一下 這個漢諾塔程序的步驟
python利用遞歸解決漢諾塔問題,求大神解釋一下代碼
這是一個典型的遞歸程序
當只有一層的時候,直接把x放到z上結束
當大於1層的時候,先把x和z放到y上,然後繼續遞歸
把y放到x上,然後放到z上,結束處理
python解決漢諾塔問題?
解漢諾塔最簡單的做法就是遞歸:
類似如何將大象裝進冰箱:1)將冰箱門打開;2)把大大象放進去;3)把冰箱門關上……
我們將所有的盤都在同一個桿上從大到小排列視為【完美狀態】,那麼,目標就是將最大碟片為n的完美狀態從a桿移到b桿,套用裝大象的思路,這個問題同樣是三步:
1)把n-1的完美狀態移到另一個桿上;
2)把n移到目標杆上;
3)把n-1的完美狀態移到目標杆上。
如下:
標題:用Python編碼描述漢諾塔步驟
#-*- coding:utf-8 -*-
count = 0
def hano():
def hanoi(n,x,y,z):
global count
count += 1
if n == 1:
print(‘Monving %d’ % n, ‘from ‘,x,’to’,z)
else:
hanoi(n-1,x,z,y)
print(‘Monving %d’ % n, ‘from ‘,x,’to’,z)
hanoi(n-1,y,x,z)
return hanoi
n = int(input(“請輸入漢諾塔的層數 :”))
hano()(n,’source’,’helper’,’target’)
print(“The total number of steps required is: “,str(count))
———–分-割-線-是-我—————-
複製分割線以上的代碼,保存為hannoi.py,在python 3 下運行,得到結果如題所示。
【python】漢諾塔遞歸
系統自帶的演示代碼,可以研究一下
#!/usr/bin/env python3
“”” turtle-example-suite:
tdemo_minimal_hanoi.py
A minimal ‘Towers of Hanoi’ animation:
A tower of 6 discs is transferred from the
left to the right peg.
An imho quite elegant and concise
implementation using a tower class, which
is derived from the built-in type list.
Discs are turtles with shape “square”, but
stretched to rectangles by shapesize()
—————————————
To exit press STOP button
—————————————
“””
from turtle import *
class Disc(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()
class Tower(list):
“Hanoi tower, a subclass of built-in type list”
def __init__(self, x):
“create an empty tower. x is x-position of peg”
self.x = x
def push(self, d):
d.setx(self.x)
d.sety(-150+34*len(self))
self.append(d)
def pop(self):
d = list.pop(self)
d.sety(150)
return d
def hanoi(n, from_, with_, to_):
if n 0:
hanoi(n-1, from_, to_, with_)
to_.push(from_.pop())
hanoi(n-1, with_, from_, to_)
def play():
onkey(None,”space”)
clear()
try:
hanoi(6, t1, t2, t3)
write(“press STOP button to exit”,
align=”center”, font=(“Courier”, 16, “bold”))
except Terminator:
pass # turtledemo user pressed STOP
def main():
global t1, t2, t3
ht(); penup(); goto(0, -225) # writer turtle
t1 = Tower(-250)
t2 = Tower(0)
t3 = Tower(250)
# make tower of 6 discs
for i in range(6,0,-1):
t1.push(Disc(i))
# prepare spartanic user interface ;-)
write(“press spacebar to start game”,
align=”center”, font=(“Courier”, 16, “bold”))
onkey(play, “space”)
listen()
return “EVENTLOOP”
if __name__==”__main__”:
msg = main()
print(msg)
mainloop()
Python 中關於漢諾塔的問題,這個程序,給定參數n的數值,怎麼在程序里運行
有a,b,c 三個柱子,有n個從大到小的盤子,大盤子必須一直放在小盤子的下面,藉助柱子b將n個盤子從a移到c
這個問題可以分解成下面的子題
先藉助柱子c,將n-1個盤子從a移到b: hanoi(n-1,a,c,b)
將第n個盤子從a移到c :print(a,’–‘,c)
然後借柱子 a將已經移致b上的n-1個盤子移到c上:hanoi(n-1,b,a,c)
這樣就移好了,將原問題分解成規模更小同樣的子問題,遞歸解決,
求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) 自循環
循環完畢,輸出
你這段代碼也是類似自循環
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/247565.html