濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > python繪制漢諾塔

python繪制漢諾塔

熱門標(biāo)簽:阿里電話機(jī)器人對(duì)話 正安縣地圖標(biāo)注app 遼寧智能外呼系統(tǒng)需要多少錢 400電話申請(qǐng)資格 電銷機(jī)器人系統(tǒng)廠家鄭州 qt百度地圖標(biāo)注 螳螂科技外呼系統(tǒng)怎么用 舉辦過(guò)冬奧會(huì)的城市地圖標(biāo)注 地圖地圖標(biāo)注有嘆號(hào)

本文實(shí)例為大家分享了python繪制漢諾塔的具體代碼,供大家參考,具體內(nèi)容如下

源碼:

import turtle
class Stack:
  def __init__(self):
    self.items = []
  def isEmpty(self):
    return len(self.items) == 0
  def push(self, item):
    self.items.append(item)
  def pop(self):
    return self.items.pop()
  def peek(self):
    if not self.isEmpty():
      return self.items[len(self.items) - 1]
  def size(self):
    return len(self.items)
def drawpole_3(): # 畫出漢諾塔的poles
  t = turtle.Turtle()
  t.hideturtle()
  def drawpole_1(k):
    t.up()
    t.pensize(10)
    t.speed(100)
    t.goto(400 * (k - 1), 100)
    t.down()
    t.goto(400 * (k - 1), -100)
    t.goto(400 * (k - 1) - 20, -100)
    t.goto(400 * (k - 1) + 20, -100)
  drawpole_1(0) # 畫出漢諾塔的poles[0]
  drawpole_1(1) # 畫出漢諾塔的poles[1]
  drawpole_1(2) # 畫出漢諾塔的poles[2]
def creat_plates(n): # 制造n個(gè)盤子
  plates = [turtle.Turtle() for i in range(n)]
  for i in range(n):
    plates[i].up()
    plates[i].hideturtle()
    plates[i].shape("square")
    plates[i].shapesize(1, 8 - i)
    plates[i].goto(-400, -90 + 20 * i)
    plates[i].showturtle()
  return plates
def pole_stack(): # 制造poles的棧
  poles = [Stack() for i in range(3)]
  return poles
def moveDisk(plates, poles, fp, tp): # 把poles[fp]頂端的盤子plates[mov]從poles[fp]移到poles[tp]
  mov = poles[fp].peek()
  plates[mov].goto((fp - 1) * 400, 150)
  plates[mov].goto((tp - 1) * 400, 150)
  l = poles[tp].size() # 確定移動(dòng)到底部的高度(恰好放在原來(lái)最上面的盤子上面)
  plates[mov].goto((tp - 1) * 400, -90 + 20 * l)
def moveTower(plates, poles, height, fromPole, toPole, withPole): # 遞歸放盤子
  if height >= 1:
    moveTower(plates, poles, height - 1, fromPole, withPole, toPole)
    moveDisk(plates, poles, fromPole, toPole)
    poles[toPole].push(poles[fromPole].pop())
    moveTower(plates, poles, height - 1, withPole, toPole, fromPole)
myscreen = turtle.Screen()
drawpole_3()
n = int(input("請(qǐng)輸入漢諾塔的層數(shù)并回車:\n"))
plates = creat_plates(n)
poles = pole_stack()
for i in range(n):
  poles[0].push(i)
moveTower(plates, poles, n, 0, 2, 1)
myscreen.exitonclick()

效果圖:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • 看看如何用Python繪制小米新版天價(jià)logo
  • Python繪制分類圖的方法
  • 用Python的繪圖庫(kù)(matplotlib)繪制小波能量譜
  • python opencv常用圖形繪制方法(線段、矩形、圓形、橢圓、文本)
  • python 繪制斜率圖進(jìn)行對(duì)比分析
  • 通過(guò)python讀取txt文件和繪制柱形圖的實(shí)現(xiàn)代碼
  • python爬取股票最新數(shù)據(jù)并用excel繪制樹(shù)狀圖的示例
  • 用python 繪制莖葉圖和復(fù)合餅圖
  • 教你怎么用python繪制dotplot

標(biāo)簽:淘寶好評(píng)回訪 信陽(yáng) 阜新 濟(jì)源 昭通 合肥 興安盟 隨州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python繪制漢諾塔》,本文關(guān)鍵詞  python,繪制,漢諾塔,python,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《python繪制漢諾塔》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于python繪制漢諾塔的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    耒阳市| 岳阳县| 菏泽市| 邳州市| 常宁市| 胶南市| 准格尔旗| 应用必备| 习水县| 莒南县| 太谷县| 东阳市| 苍梧县| 应用必备| 金溪县| 浪卡子县| 项城市| 阿图什市| 集安市| 竹山县| 云梦县| 钦州市| 内江市| 九龙县| 呈贡县| 富民县| 聊城市| 漠河县| 连南| 武功县| 鹤壁市| 彭水| 灵宝市| 泰州市| 富锦市| 凤凰县| 新疆| 卢氏县| 贞丰县| 晴隆县| 凤庆县|