@w616561153
2020-02-25T20:29:27.000000Z
字数 14082
阅读 526
课设-染色问题
import math
import time
import tkinter as tk
from tkinter import *
color = ['red', 'green', 'blue' ,'yellow', 'brown','puruple', 'orange', 'grey', 'pink']
root = tk.Tk()
root.geometry('600x600')
cv = Canvas(root,width = 600, height = 600, bg = 'white')
cv.pack()
G = [[] for i in range(50)]
def add_edge(u, v):
u -= 1
v -= 1
G[u].append(v)
G[v].append(u)
xx0, yy0, xx1, yy1 = 55, 55, 120, 120
n = 7
m = 4
add_edge(1, 2)
add_edge(1, 3)
add_edge(1, 4)
add_edge(2, 4)
add_edge(1, 5)
add_edge(1, 6)
add_edge(2, 5)
add_edge(2, 4)
add_edge(4, 5)
add_edge(1, 7)
add_edge(2, 7)
add_edge(6, 7)
add_edge(4, 7)
x = []
y = []
x0 = []
y0 = []
x1 = []
y1 = []
k = math.ceil(n / 2)
print(k)
for i in range(n):
x0.append(xx0 + (i % k) * 100)
x1.append(xx1 + (i % k) * 100)
y0.append(yy0 + (i >= k) * 100)
y1.append(yy1 + (i >= k) * 100)
print(x0, x1, y0, y1, sep= '\n')
for i in range(n):
x.append((x0[i] + x1[i]) / 2)
y.append((y0[i] + y1[i]) / 2)
f = 95
for u in range(n):
for v in G[u]:
if u < k and v < k:
cv.create_arc(x[u], y0[u] + f, x[v], y1[v] - f, extent = 180, style = ARC)
elif u >= k and v >= k:
cv.create_arc(x[u], y0[u] + f, x[v], y1[v] - f, extent = -180, style = ARC)
else:
cv.create_line(x[u], y[u], x[v], y[v])
for i in range(n):
cv.create_oval(x0[i], y0[i], x1[i], y1[i], fill = 'white')
cv.create_text(x[i], y[i], text = str(i + 1))
vis = [-1 for i in range(20)]
ans = 0
def dfs(now):
global ans
if now == n:
ans = 1
return
for i in range(m):
vis[now] = i
cv.create_oval(x0[now], y0[now], x1[now], y1[now], fill = color[i])
cv.create_text(x[now], y[now], text = str(now + 1))
root.update()
time.sleep(0.5)
flag = 1
for v in G[now]:
if vis[v] == i:
flag = 0
break
if flag:
dfs(now + 1)
if ans:
return
dfs(0)
root.mainloop()
我要做一个养生少年。
import math
import time
import tkinter as tk
import tkinter.messagebox
from tkinter import *
window = tk.Tk()
window.title("着色问题")
window.geometry("800x800")
n = 0
e = 0
m = 0
flag = 0
ans = 0
G = [[] for i in range(50)]
def add_edge(u, v):
u -= 1
v -= 1
G[u].append(v)
G[v].append(u)
def insert_point():
global n, e, m, flag
n = int(e1.get())
e = int(e2.get())
m = int(e3.get())
if not(1 <= n <= 10 and 1 <= e <= 20 and 1 <= m <= 10):
tkinter.messagebox.showwarning(title='警告', message='请输入正确的V,E,m。')
e1.delete(0, 'end')
e2.delete(0, 'end')
e3.delete(0, 'end')
return
flag = 1
l8 = tk.Label(window, text='(还剩余:' + str(e) + ')')
l8.place(x=565, y=120 + move_down, anchor='nw')
l13 = tk.Label(window, text=' <= ' + str(n), font=('Arial', 12), width=10, height=2).place(x=60, y=425 + move_down,
anchor='nw')
l14 = tk.Label(window, text=' <= ' + str(n), font=('Arial', 12), width=10, height=2).place(x=365, y=425 + move_down,
anchor='nw')
t.delete('1.0', 'end')
def insert_point1():
global n, e, m
if not n and not e and not m:
tkinter.messagebox.showwarning(title='警告', message='请先输入V, E, m。')
return
u = int(e4.get())
v = int(e5.get())
e4.delete(0, 'end')
e5.delete(0, 'end')
if not(1 <= u <= n and 1 <= v <= n):
tkinter.messagebox.showwarning(title='警告', message='请先输入正确的起点和终点。')
return
if (v - 1) in G[u - 1]:
tkinter.messagebox.showwarning(title='警告', message='这条边已经被输入过了。')
return
e -= 1
add_edge(u, v)
l8 = tk.Label(window, text='(还剩余:' + str(e) + ')').place(x=565, y=120 + move_down, anchor='nw')
t.insert('insert', ' ' + str(u) + '---' + str(v) + '\n')
if e == 0:
color = ['red', 'green', 'blue', 'yellow', 'brown', 'puruple', 'orange', 'grey', 'pink']
root = tk.Tk()
root.title('染色过程')
root.geometry('600x600')
cv = Canvas(root, width=600, height=600, bg='white')
cv.pack()
xx0, yy0, xx1, yy1 = 55, 55, 120, 120
x = []
y = []
x0 = []
y0 = []
x1 = []
y1 = []
k = math.ceil(n / 2)
for i in range(n):
x0.append(xx0 + (i % k) * 100)
x1.append(xx1 + (i % k) * 100)
y0.append(yy0 + (i >= k) * 150)
y1.append(yy1 + (i >= k) * 150)
for i in range(n):
x.append((x0[i] + x1[i]) / 2)
y.append((y0[i] + y1[i]) / 2)
f = 95
for u in range(n):
for v in G[u]:
if u < k and v < k:
cv.create_arc(x[u], y0[u] + f, x[v], y1[v] - f, extent=180, style=ARC)
elif u >= k and v >= k:
cv.create_arc(x[u], y0[u] + f, x[v], y1[v] - f, extent=-180, style=ARC)
else:
cv.create_line(x[u], y[u], x[v], y[v])
for i in range(n):
cv.create_oval(x0[i], y0[i], x1[i], y1[i], fill='white')
cv.create_text(x[i], y[i], text=str(i + 1))
vis = [-1 for i in range(20)]
def dfs(now):
global ans
if now == n:
ans = 1
return
for i in range(m):
vis[now] = i
cv.create_oval(x0[now], y0[now], x1[now], y1[now], fill=color[i])
cv.create_text(x[now], y[now], text=str(now + 1))
root.update()
time.sleep(0.5)
flag = 1
for v in G[now]:
if vis[v] == i:
flag = 0
break
if flag:
dfs(now + 1)
if ans:
return
dfs(0)
global ans
if ans == 0:
tkinter.messagebox.showinfo(title='失败', message = '用' + str(m) + '种颜色染色失败!')
else:
maxx = 0
for i in range(n):
maxx = max(vis[i], maxx)
tkinter.messagebox.showinfo(title='成功', message='共用' + str(maxx + 1) + '种颜色成功染色!')
root.mainloop()
move_down = 55
l1 = tk.Label(window, text='图的结点数V', bg='green', font=('Arial', 12), width=20, height=2)
l1.place(x = 0, y = 60 + move_down, anchor = 'nw')
l2 = tk.Label(window, text='图的边数E', bg='green', font=('Arial', 12), width=20, height=2)
l2.place(x = 0, y = 120 + move_down, anchor = 'nw')
l3 = tk.Label(window, text = '染色最多需要的颜色数m', bg = 'green', font = ('Arial', 12), width = 20, height = 2)
l3.place(x = 0, y = 180 + move_down, anchor = 'nw')
e1 = tk.Entry(window, show = None)
e1.place(x = 190, y = 70 + move_down, anchor = 'nw')
l10 = tk.Label(window, text = ' <= 10', font = ('Arial', 12), width = 10, height = 2).place(x = 320, y = 60 + move_down, anchor = 'nw')
e2 = tk.Entry(window, show = None)
l11 = tk.Label(window, text = ' <= 20', font = ('Arial', 12), width = 10, height = 2).place(x = 320, y = 120 + move_down, anchor = 'nw')
e2.place(x = 190, y = 130 + move_down, anchor = 'nw')
l12 = tk.Label(window, text = ' <= 10', font = ('Arial', 12), width = 10, height = 2).place(x = 320, y = 180 + move_down, anchor = 'nw')
e3 = tk.Entry(window, show = None)
e3.place(x = 190, y = 190 + move_down, anchor = 'nw')
b1 = tk.Button(window, text='确认', width=10,
height=1, command=insert_point)
b1.place(x = 150, y = 240 + move_down, anchor = 'nw')
l4 = tk.Label(window, text='边的起点与终点', bg='green', font=('Arial', 12), width=20, height=2)
l4.place(x=80, y=330 + move_down, anchor='nw')
l5 = tk.Label(window, text='起点', bg='green', font=('Arial', 12), width=5, height=1)
l5.place(x=0, y=385 + move_down, anchor='nw')
l6 = tk.Label(window, text='终点', bg='green', font=('Arial', 12), width=5, height=1)
l6.place(x=300, y=385 + move_down, anchor='nw')
t = tk.Text(window, width=12, height=40, bg='yellow')
t.place(x=560, y=150 + move_down, anchor='nw')
l7 = tk.Label(window, text='已加入的边', bg='green', font=('Arial', 12), width=15, height=2)
l7.place(x=530, y=70 + move_down, anchor='nw')
l8 = tk.Label(window, text=' E未知')
l8.place(x=565, y=120 + move_down, anchor='nw')
e4 = tk.Entry(window, width=10, show=None)
e4.place(x=0, y=435 + move_down, anchor='nw')
e5 = tk.Entry(window, width=10, show=None)
e5.place(x=300, y=435 + move_down, anchor='nw')
b2 = tk.Button(window, text='确认', width=10, height=1, command = insert_point1)
b2.place(x=150, y=480 + move_down, anchor='nw')
l9 = tk.Label(window, text = '图染色问题可视化模拟', font = ('Arial', 20)).pack()
window.mainloop()
import math
import time
import tkinter as tk
import tkinter.messagebox
from tkinter import *
window = tk.Tk()
window.title("着色问题")
window.geometry("800x800")
n = 0
e = 0
m = 0
flag = 0
ans = 0
G = [[] for i in range(50)]
color = ['red', 'green', 'blue' ,'yellow', 'brown','puruple', 'orange', 'grey', 'pink']
xx0, yy0, xx1, yy1 = 55, 55, 120, 120
x = []
y = []
x0 = []
y0 = []
x1 = []
y1 = []
k = 0
vis = [-1 for i in range(20)]
ans = 0
cv = 0
root = 0
def add_edge(u, v):
u -= 1
v -= 1
G[u].append(v)
G[v].append(u)
def init_vertex(): #这个函数是画结点.
# x0, x1, y0, y1, x, y都是坐标.得先明白一下cv.create_oval(), cv.create_text(), cv.create_arc(), cv.create_line()这几个函数.
# 见名知意.cv.create_oval()就是画椭圆(圆),前两个参数是左上角的横纵坐标,后两个参数是右下角的横纵坐标,最后一个fill=是颜色.如果我们使左上角,右下角围起来的是一个正方形,那么cv.create_oval()画出来的就是一个圆.
# cv.create_text()就是放置文本.前两个参数就是文本的横纵坐标,最后一个text=字符串就是文本.
# cv.create_arc()是画弓形(曲线),前四个参数和cv.create_oval()的意义相同.extent = 弧度制,就是你要画的那部分,180就是上半部分,-180就是下半部分.
# cv.create_line()是画直线,前两个参数是一个端点的横纵坐标和另一个端点的横纵坐标.
global cv
for i in range(n):
x0.append(xx0 + (i % k) * 100) # 每个圆中间间隔100.
x1.append(xx1 + (i % k) * 100)
y0.append(yy0 + (i >= k) * 100) #前一半的圆在上面,后一半的圆在下面.
y1.append(yy1 + (i >= k) * 100)
for i in range(n):
x.append((x0[i] + x1[i]) / 2) #圆心坐标,标号用.
y.append((y0[i] + y1[i]) / 2)
print(x0, x1, y1)
for i in range(n):
cv.create_oval(x0[i], y0[i], x1[i], y1[i], fill = 'white') #画圆.
cv.create_text(x[i], y[i], text = str(i + 1)) #标号.
def init_edge(): # 这个函数是结点之间的连线.
global cv
f = 95
for u in range(n):
for v in G[u]:
if u < k and v < k: # 都在上面的就拿弧的上半部分连线.
cv.create_arc(x[u], y0[u] + f, x[v], y1[v] - f, extent=180, style=ARC)
elif u >= k and v >= k: # 都在下面的就拿弧的下半部分连线.
cv.create_arc(x[u], y0[u] + f, x[v], y1[v] - f, extent=-180, style=ARC)
else: # 不在同一排就连直线.
cv.create_line(x[u], y[u], x[v], y[v])
for i in range(n): # 再画一遍圆,把圆内的线盖上.
cv.create_oval(x0[i], y0[i], x1[i], y1[i], fill='white')
cv.create_text(x[i], y[i], text=str(i + 1))
def dfs(now):
global ans, cv, root
if now == n:
ans = 1
return
for i in range(m):
vis[now] = i
cv.create_oval(x0[now], y0[now], x1[now], y1[now], fill = color[i]) #染色过程,给圆染上第i种颜色.
cv.create_text(x[now], y[now], text = str(now + 1)) #再把号标上.
root.update()
time.sleep(0.5) #每一步停半秒.
flag = 1
for v in G[now]:
if vis[v] == i:
flag = 0
break
if flag:
dfs(now + 1)
if ans:
return
def colored():
global k, cv, root
root = tk.Tk() # 先创建一个窗口.
root.geometry('600x600') # 设置窗口大小.
cv = Canvas(root, width=600, height=600, bg='white') # 创建一个白色画布,用来在上面画画.
cv.pack() # 把它放在窗口上.(没有side参数就默认放在窗口的正上部.)
k = math.ceil(n / 2)
init_vertex()
init_edge()
dfs(0)
global ans
if ans == 0:
tkinter.messagebox.showinfo(title='失败', message='用' + str(m) + '种颜色染色失败!')
else:
maxx = 0
for i in range(n):
maxx = max(vis[i], maxx)
tkinter.messagebox.showinfo(title='成功', message='共用' + str(maxx + 1) + '种颜色成功染色!')
root.mainloop()
def insert_point():
global n, e, m, flag
n = int(e1.get())
e = int(e2.get())
m = int(e3.get())
if not(1 <= n <= 10 and 1 <= e <= 20 and 1 <= m <= 10):
tkinter.messagebox.showwarning(title='警告', message='请输入正确的V,E,m。')
e1.delete(0, 'end')
e2.delete(0, 'end')
e3.delete(0, 'end')
return
flag = 1
l8 = tk.Label(window, text='(还剩余:' + str(e) + ')')
l8.place(x=565, y=120 + move_down, anchor='nw')
l13 = tk.Label(window, text=' <= ' + str(n), font=('Arial', 12), width=10, height=2).place(x=60, y=425 + move_down,
anchor='nw')
l14 = tk.Label(window, text=' <= ' + str(n), font=('Arial', 12), width=10, height=2).place(x=365, y=425 + move_down,
anchor='nw')
t.delete('1.0', 'end')
def insert_point1():
global n, e, m
if not n and not e and not m:
tkinter.messagebox.showwarning(title='警告', message='请先输入V, E, m。')
return
u = int(e4.get())
v = int(e5.get())
e4.delete(0, 'end')
e5.delete(0, 'end')
if not(1 <= u <= n and 1 <= v <= n):
tkinter.messagebox.showwarning(title='警告', message='请先输入正确的起点和终点。')
return
if (v - 1) in G[u - 1]:
tkinter.messagebox.showwarning(title='警告', message='这条边已经被输入过了。')
return
e -= 1
add_edge(u, v)
l8 = tk.Label(window, text='(还剩余:' + str(e) + ')').place(x=565, y=120 + move_down, anchor='nw')
t.insert('insert', ' ' + str(u) + '---' + str(v) + '\n')
if e == 0:
colored()
move_down = 55
l1 = tk.Label(window, text='图的结点数V', bg='green', font=('Arial', 12), width=20, height=2)
l1.place(x = 0, y = 60 + move_down, anchor = 'nw')
l2 = tk.Label(window, text='图的边数E', bg='green', font=('Arial', 12), width=20, height=2)
l2.place(x = 0, y = 120 + move_down, anchor = 'nw')
l3 = tk.Label(window, text = '染色最多需要的颜色数m', bg = 'green', font = ('Arial', 12), width = 20, height = 2)
l3.place(x = 0, y = 180 + move_down, anchor = 'nw')
e1 = tk.Entry(window, show = None)
e1.place(x = 190, y = 70 + move_down, anchor = 'nw')
l10 = tk.Label(window, text = ' <= 10', font = ('Arial', 12), width = 10, height = 2).place(x = 320, y = 60 + move_down, anchor = 'nw')
e2 = tk.Entry(window, show = None)
l11 = tk.Label(window, text = ' <= 20', font = ('Arial', 12), width = 10, height = 2).place(x = 320, y = 120 + move_down, anchor = 'nw')
e2.place(x = 190, y = 130 + move_down, anchor = 'nw')
l12 = tk.Label(window, text = ' <= 10', font = ('Arial', 12), width = 10, height = 2).place(x = 320, y = 180 + move_down, anchor = 'nw')
e3 = tk.Entry(window, show = None)
e3.place(x = 190, y = 190 + move_down, anchor = 'nw')
b1 = tk.Button(window, text='确认', width=10,
height=1, command=insert_point)
b1.place(x = 150, y = 240 + move_down, anchor = 'nw')
l4 = tk.Label(window, text='边的起点与终点', bg='green', font=('Arial', 12), width=20, height=2)
l4.place(x=80, y=330 + move_down, anchor='nw')
l5 = tk.Label(window, text='起点', bg='green', font=('Arial', 12), width=5, height=1)
l5.place(x=0, y=385 + move_down, anchor='nw')
l6 = tk.Label(window, text='终点', bg='green', font=('Arial', 12), width=5, height=1)
l6.place(x=300, y=385 + move_down, anchor='nw')
t = tk.Text(window, width=12, height=40, bg='yellow')
t.place(x=560, y=150 + move_down, anchor='nw')
l7 = tk.Label(window, text='已加入的边', bg='green', font=('Arial', 12), width=15, height=2)
l7.place(x=530, y=70 + move_down, anchor='nw')
l8 = tk.Label(window, text=' E未知')
l8.place(x=565, y=120 + move_down, anchor='nw')
e4 = tk.Entry(window, width=10, show=None)
e4.place(x=0, y=435 + move_down, anchor='nw')
e5 = tk.Entry(window, width=10, show=None)
e5.place(x=300, y=435 + move_down, anchor='nw')
b2 = tk.Button(window, text='确认', width=10, height=1, command = insert_point1)
b2.place(x=150, y=480 + move_down, anchor='nw')
l9 = tk.Label(window, text = '图染色问题可视化模拟', font = ('Arial', 20)).pack()
window.mainloop()
import math
import time
import tkinter as tk #用python中的tkinter模块和其中的Canvas画布控件.
from tkinter import *
# 用dfs深度搜索来解决这个问题.
color = ['red', 'green', 'blue' ,'yellow', 'brown','puruple', 'orange', 'grey', 'pink']
root = tk.Tk() # 先创建一个窗口.
root.geometry('600x600') #设置窗口大小.
cv = Canvas(root,width = 600, height = 600, bg = 'white') # 创建一个白色画布,用来在上面画画.
cv.pack() # 把它放在窗口上.(没有side参数就默认放在窗口的正上部.)
n = 4
m = 4
xx0, yy0, xx1, yy1 = 55, 55, 120, 120 # 画圆的起始坐标,第一个圆的左上角和右下角.
x = []
y = []
x0 = []
y0 = []
x1 = []
y1 = []
k = math.ceil(n / 2) #ceil()是向上取整函数,例如ceil(2.5) = 3, ceil(2.01) = 3, ceil(2) = 2.
G = [[] for i in range(50)] # 这个就是c++中的vector<int> G[50],用来存图的.
def add_edge(u, v):
u -= 1 #u--,v-- 是因为输入的是1 -- 2,但我们存下来的实际是0 -- 1.
v -= 1
G[u].append(v) # 无向边,正着来一边,反着来一遍.
G[v].append(u)
def init_vertex(): #这个函数是画结点.
# x0, x1, y0, y1, x, y都是坐标.得先明白一下cv.create_oval(), cv.create_text(), cv.create_arc(), cv.create_line()这几个函数.
# 见名知意.cv.create_oval()就是画椭圆(圆),前两个参数是左上角的横纵坐标,后两个参数是右下角的横纵坐标,最后一个fill=是颜色.如果我们使左上角,右下角围起来的是一个正方形,那么cv.create_oval()画出来的就是一个圆.
# cv.create_text()就是放置文本.前两个参数就是文本的横纵坐标,最后一个text=字符串就是文本.
# cv.create_arc()是画弓形(曲线),前四个参数和cv.create_oval()的意义相同.extent = 弧度制,就是你要画的那部分,180就是上半部分,-180就是下半部分.
# cv.create_line()是画直线,前两个参数是一个端点的横纵坐标和另一个端点的横纵坐标.
for i in range(n):
x0.append(xx0 + (i % k) * 100) # 每个圆中间间隔100.
x1.append(xx1 + (i % k) * 100)
y0.append(yy0 + (i >= k) * 100) #前一半的圆在上面,后一半的圆在下面.
y1.append(yy1 + (i >= k) * 100)
for i in range(n):
x.append((x0[i] + x1[i]) / 2) #圆心坐标,标号用.
y.append((y0[i] + y1[i]) / 2)
for i in range(n):
cv.create_oval(x0[i], y0[i], x1[i], y1[i], fill = 'white') #画圆.
cv.create_text(x[i], y[i], text = str(i + 1)) #标号.
def init_edge(): # 这个函数是结点之间的连线.
f = 95
for u in range(n):
for v in G[u]:
if u < k and v < k: # 都在上面的就拿弧的上半部分连线.
cv.create_arc(x[u], y0[u] + f, x[v], y1[v] - f, extent = 180, style = ARC)
elif u >= k and v >= k: # 都在下面的就拿弧的下半部分连线.
cv.create_arc(x[u], y0[u] + f, x[v], y1[v] - f, extent = -180, style = ARC)
else: # 不在同一排就连直线.
cv.create_line(x[u], y[u], x[v], y[v])
for i in range(n): #再画一遍圆,把圆内的线盖上.
cv.create_oval(x0[i], y0[i], x1[i], y1[i], fill = 'white')
cv.create_text(x[i], y[i], text = str(i + 1))
vis = [-1 for i in range(20)]
ans = 0
def dfs(now):
global ans
if now == n:
ans = 1
return
for i in range(m):
vis[now] = i
cv.create_oval(x0[now], y0[now], x1[now], y1[now], fill = color[i]) #染色过程,给圆染上第i种颜色.
cv.create_text(x[now], y[now], text = str(now + 1)) #再把号标上.
root.update()
time.sleep(0.5) #每一步停半秒.
flag = 1
for v in G[now]:
if vis[v] == i:
flag = 0
break
if flag:
dfs(now + 1)
if ans:
return
add_edge(1, 2)
add_edge(1, 3)
add_edge(1, 4)
add_edge(2, 4)
init_vertex()
init_edge()
dfs(0)
root.mainloop()
老板,还有什么要加上的吗?