zoukankan      html  css  js  c++  java
  • 【Python】用turtle库动态显示汉诺塔

     一.初步了解汉诺塔问题


    汉诺塔问题,想必学过C语言的朋友都有过了解,其最大的一个特点是运用递归算法

    一座汉诺塔,塔内有3个座A、B、C,A座上有n个盘子,盘子大小不等,大的在下,小的在上,如图所示。把这n个盘子从A座移到C座,但每次只能移动一个盘子,并且自移动过程中,3个座上的盘子始终保持大盘在下,小盘在上。在移动过程中可以利用B座来放盘子。

    二.Python静态实现

    1.代码如下:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪

    def Hanoi(n,A,B,C):
        if n== 1:
            print(A,'-->',C)
        else:
            Hanoi(n-1,A,C,B)
            Hanoi(1,A,B,C)
            Hanoi(n-1,B,A,C)
    n= input()
    Hanoi(int(n),'A','B','C')
    

    2.运行结果如下:

     

    三.运用Turtle实现汉诺塔的可视化运行

    1.代码如下:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪

    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(250*(k-1), 100)
            t.down()
            t.goto(250*(k-1), -100)
            t.goto(250*(k-1)-20, -100)
            t.goto(250*(k-1)+20, -100)
        drawpole_1(0)#画出汉诺塔的polesA
        drawpole_1(1)#画出汉诺塔的polesB
        drawpole_1(2)#画出汉诺塔的polesC
    
    def creat_plates(n):#制造n个盘子
        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,9-i)
            plates[i].goto(-250,-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):#把polesA顶端的盘子plates[mov]从polesA移到polesC
        mov=poles[fp].peek()
        plates[mov].goto((fp-1)*250,150)
        plates[mov].goto((tp-1)*250,150)
        l=poles[tp].size()        #确定移动到底部的高度(恰好放在原来最上面的盘子上面)
        plates[mov].goto((tp-1)*250,-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("请输入汉诺塔的层数并回车:
    "))
    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()
    

     2.运行结果如下: 

     

    -------------------最后模型---------------

    四.总结 

    1.熟悉递归算法的运用

    2.能熟练使用turtle库

  • 相关阅读:
    (zhuan) How to Train Neural Networks With Backpropagation
    (转)Introduction to Gradient Descent Algorithm (along with variants) in Machine Learning
    Learning to Compare Image Patches via Convolutional Neural Networks --- Reading Summary
    Unsupervised Image-to-Image Translation Networks --- Reading Writing
    (转) RNN models for image generation
    (zhuan) 126 篇殿堂级深度学习论文分类整理 从入门到应用
    (zhuan) Where can I start with Deep Learning?
    (zhuan) LSTM Neural Network for Time Series Prediction
    (zhuan) awesome-object-proposals
    Face Aging with Conditional Generative Adversarial Network 论文笔记
  • 原文地址:https://www.cnblogs.com/yyp-20190107/p/10623139.html
Copyright © 2011-2022 走看看