资源简介

本程序用python实现,采用动态规划算法实现数塔问题,并实现界面,实现10层数塔的输入,走过的路径将采用红色背景显示,输出走过路径的最小值。

资源截图

代码片段和文件信息

# coding=gbk

import tkinter as tk
import tkinter.messagebox

# 更改背景颜色函数
def color(i j):
    if i==0:
        if j==0: e1.config(bg=‘red‘)
    elif i==1: 
        if j==0: e2.config(bg=‘red‘)
        elif j==1: e3.config(bg=‘red‘)
    elif i==2:
        if j==0: e4.config(bg=‘red‘)
        elif j==1: e5.config(bg=‘red‘)
        elif j==2: e6.config(bg=‘red‘)
    elif i==3:
        if j==0: e7.config(bg=‘red‘)
        elif j==1: e8.config(bg=‘red‘)
        elif j==2: e9.config(bg=‘red‘)
        elif j==3: e10.config(bg=‘red‘)
    elif i==4:
        if j==0: e11.config(bg=‘red‘)
        elif j==1: e12.config(bg=‘red‘)
        elif j==2: e13.config(bg=‘red‘)
        elif j==3: e14.config(bg=‘red‘)
        elif j==4: e15.config(bg=‘red‘)
    elif i==5:
        if j==0: e16.config(bg=‘red‘)
        elif j==1: e17.config(bg=‘red‘)
        elif j==2: e18.config(bg=‘red‘)
        elif j==3: e19.config(bg=‘red‘)
        elif j==4: e20.config(bg=‘red‘)
        elif j==5: e21.config(bg=‘red‘)
    elif i==6:
        if j==0: e22.config(bg=‘red‘)
        elif j==1: e23.config(bg=‘red‘)
        elif j==2: e24.config(bg=‘red‘)
        elif j==3: e25.config(bg=‘red‘)
        elif j==4: e26.config(bg=‘red‘)
        elif j==5: e27.config(bg=‘red‘)
        elif j==6: e28.config(bg=‘red‘)
    elif i==7:
        if j==0: e29.config(bg=‘red‘)
        elif j==1: e30.config(bg=‘red‘)
        elif j==2: e31.config(bg=‘red‘)
        elif j==3: e32.config(bg=‘red‘)
        elif j==4: e33.config(bg=‘red‘)
        elif j==5: e34.config(bg=‘red‘)
        elif j==6: e35.config(bg=‘red‘)
        elif j==7: e36.config(bg=‘red‘)
    elif i==8:
        if j==0: e37.config(bg=‘red‘)
        elif j==1: e38.config(bg=‘red‘)
        elif j==2: e39.config(bg=‘red‘)
        elif j==3: e40.config(bg=‘red‘)
        elif j==4: e41.config(bg=‘red‘)
        elif j==5: e42.config(bg=‘red‘)
        elif j==6: e43.config(bg=‘red‘)
        elif j==7: e44.config(bg=‘red‘)
        elif j==8: e45.config(bg=‘red‘)
    elif i==9:
        if j==0: e46.config(bg=‘red‘)
        elif j==1: e47.config(bg=‘red‘)
        elif j==2: e48.config(bg=‘red‘)
        elif j==3: e49.config(bg=‘red‘)
        elif j==4: e50.config(bg=‘red‘)
        elif j==5: e51.config(bg=‘red‘)
        elif j==6: e52.config(bg=‘red‘)
        elif j==7: e53.config(bg=‘red‘)
        elif j==8: e54.config(bg=‘red‘)
        elif j==9: e55.config(bg=‘red‘)
    
def cpy():
    global var
    t=e.get()
    # 异常设置
    if t==‘‘:
        tkinter.messagebox.showerror(title=‘错误‘ message=‘请输入层数‘)
        return 
    
    # 录入数据
    matrix[0][0]=e1.get()
    matrix[1][0]=e2.get()
    matrix[1][1]=e3.get()
    matrix[2][0]=e4.get()
    matrix[2][1]=e5.get()
    matrix[2][2]=e6.get()
    matrix[3][0]=e7.get()
    matrix[3][1]=e8.get()
    matrix[3][2]=e9.get()
    matrix[3][3]=e10.get()
    matrix[4][0]=e11.get()
    matrix[4][1]=e12.get()
    matrix[4]

评论

共有 条评论