[关闭]
@w616561153 2020-02-25T20:29:27.000000Z 字数 14082 阅读 526

课设:染色问题可视化

课设-染色问题


2020.2.22 染色 python3 + tkinter Canvas

  1. import math
  2. import time
  3. import tkinter as tk
  4. from tkinter import *
  5. color = ['red', 'green', 'blue' ,'yellow', 'brown','puruple', 'orange', 'grey', 'pink']
  6. root = tk.Tk()
  7. root.geometry('600x600')
  8. cv = Canvas(root,width = 600, height = 600, bg = 'white')
  9. cv.pack()
  10. G = [[] for i in range(50)]
  11. def add_edge(u, v):
  12. u -= 1
  13. v -= 1
  14. G[u].append(v)
  15. G[v].append(u)
  16. xx0, yy0, xx1, yy1 = 55, 55, 120, 120
  17. n = 7
  18. m = 4
  19. add_edge(1, 2)
  20. add_edge(1, 3)
  21. add_edge(1, 4)
  22. add_edge(2, 4)
  23. add_edge(1, 5)
  24. add_edge(1, 6)
  25. add_edge(2, 5)
  26. add_edge(2, 4)
  27. add_edge(4, 5)
  28. add_edge(1, 7)
  29. add_edge(2, 7)
  30. add_edge(6, 7)
  31. add_edge(4, 7)
  32. x = []
  33. y = []
  34. x0 = []
  35. y0 = []
  36. x1 = []
  37. y1 = []
  38. k = math.ceil(n / 2)
  39. print(k)
  40. for i in range(n):
  41. x0.append(xx0 + (i % k) * 100)
  42. x1.append(xx1 + (i % k) * 100)
  43. y0.append(yy0 + (i >= k) * 100)
  44. y1.append(yy1 + (i >= k) * 100)
  45. print(x0, x1, y0, y1, sep= '\n')
  46. for i in range(n):
  47. x.append((x0[i] + x1[i]) / 2)
  48. y.append((y0[i] + y1[i]) / 2)
  49. f = 95
  50. for u in range(n):
  51. for v in G[u]:
  52. if u < k and v < k:
  53. cv.create_arc(x[u], y0[u] + f, x[v], y1[v] - f, extent = 180, style = ARC)
  54. elif u >= k and v >= k:
  55. cv.create_arc(x[u], y0[u] + f, x[v], y1[v] - f, extent = -180, style = ARC)
  56. else:
  57. cv.create_line(x[u], y[u], x[v], y[v])
  58. for i in range(n):
  59. cv.create_oval(x0[i], y0[i], x1[i], y1[i], fill = 'white')
  60. cv.create_text(x[i], y[i], text = str(i + 1))
  61. vis = [-1 for i in range(20)]
  62. ans = 0
  63. def dfs(now):
  64. global ans
  65. if now == n:
  66. ans = 1
  67. return
  68. for i in range(m):
  69. vis[now] = i
  70. cv.create_oval(x0[now], y0[now], x1[now], y1[now], fill = color[i])
  71. cv.create_text(x[now], y[now], text = str(now + 1))
  72. root.update()
  73. time.sleep(0.5)
  74. flag = 1
  75. for v in G[now]:
  76. if vis[v] == i:
  77. flag = 0
  78. break
  79. if flag:
  80. dfs(now + 1)
  81. if ans:
  82. return
  83. dfs(0)
  84. root.mainloop()

我要做一个养生少年。

2020.2.23 输入 + 染色

  1. import math
  2. import time
  3. import tkinter as tk
  4. import tkinter.messagebox
  5. from tkinter import *
  6. window = tk.Tk()
  7. window.title("着色问题")
  8. window.geometry("800x800")
  9. n = 0
  10. e = 0
  11. m = 0
  12. flag = 0
  13. ans = 0
  14. G = [[] for i in range(50)]
  15. def add_edge(u, v):
  16. u -= 1
  17. v -= 1
  18. G[u].append(v)
  19. G[v].append(u)
  20. def insert_point():
  21. global n, e, m, flag
  22. n = int(e1.get())
  23. e = int(e2.get())
  24. m = int(e3.get())
  25. if not(1 <= n <= 10 and 1 <= e <= 20 and 1 <= m <= 10):
  26. tkinter.messagebox.showwarning(title='警告', message='请输入正确的V,E,m。')
  27. e1.delete(0, 'end')
  28. e2.delete(0, 'end')
  29. e3.delete(0, 'end')
  30. return
  31. flag = 1
  32. l8 = tk.Label(window, text='(还剩余:' + str(e) + ')')
  33. l8.place(x=565, y=120 + move_down, anchor='nw')
  34. l13 = tk.Label(window, text=' <= ' + str(n), font=('Arial', 12), width=10, height=2).place(x=60, y=425 + move_down,
  35. anchor='nw')
  36. l14 = tk.Label(window, text=' <= ' + str(n), font=('Arial', 12), width=10, height=2).place(x=365, y=425 + move_down,
  37. anchor='nw')
  38. t.delete('1.0', 'end')
  39. def insert_point1():
  40. global n, e, m
  41. if not n and not e and not m:
  42. tkinter.messagebox.showwarning(title='警告', message='请先输入V, E, m。')
  43. return
  44. u = int(e4.get())
  45. v = int(e5.get())
  46. e4.delete(0, 'end')
  47. e5.delete(0, 'end')
  48. if not(1 <= u <= n and 1 <= v <= n):
  49. tkinter.messagebox.showwarning(title='警告', message='请先输入正确的起点和终点。')
  50. return
  51. if (v - 1) in G[u - 1]:
  52. tkinter.messagebox.showwarning(title='警告', message='这条边已经被输入过了。')
  53. return
  54. e -= 1
  55. add_edge(u, v)
  56. l8 = tk.Label(window, text='(还剩余:' + str(e) + ')').place(x=565, y=120 + move_down, anchor='nw')
  57. t.insert('insert', ' ' + str(u) + '---' + str(v) + '\n')
  58. if e == 0:
  59. color = ['red', 'green', 'blue', 'yellow', 'brown', 'puruple', 'orange', 'grey', 'pink']
  60. root = tk.Tk()
  61. root.title('染色过程')
  62. root.geometry('600x600')
  63. cv = Canvas(root, width=600, height=600, bg='white')
  64. cv.pack()
  65. xx0, yy0, xx1, yy1 = 55, 55, 120, 120
  66. x = []
  67. y = []
  68. x0 = []
  69. y0 = []
  70. x1 = []
  71. y1 = []
  72. k = math.ceil(n / 2)
  73. for i in range(n):
  74. x0.append(xx0 + (i % k) * 100)
  75. x1.append(xx1 + (i % k) * 100)
  76. y0.append(yy0 + (i >= k) * 150)
  77. y1.append(yy1 + (i >= k) * 150)
  78. for i in range(n):
  79. x.append((x0[i] + x1[i]) / 2)
  80. y.append((y0[i] + y1[i]) / 2)
  81. f = 95
  82. for u in range(n):
  83. for v in G[u]:
  84. if u < k and v < k:
  85. cv.create_arc(x[u], y0[u] + f, x[v], y1[v] - f, extent=180, style=ARC)
  86. elif u >= k and v >= k:
  87. cv.create_arc(x[u], y0[u] + f, x[v], y1[v] - f, extent=-180, style=ARC)
  88. else:
  89. cv.create_line(x[u], y[u], x[v], y[v])
  90. for i in range(n):
  91. cv.create_oval(x0[i], y0[i], x1[i], y1[i], fill='white')
  92. cv.create_text(x[i], y[i], text=str(i + 1))
  93. vis = [-1 for i in range(20)]
  94. def dfs(now):
  95. global ans
  96. if now == n:
  97. ans = 1
  98. return
  99. for i in range(m):
  100. vis[now] = i
  101. cv.create_oval(x0[now], y0[now], x1[now], y1[now], fill=color[i])
  102. cv.create_text(x[now], y[now], text=str(now + 1))
  103. root.update()
  104. time.sleep(0.5)
  105. flag = 1
  106. for v in G[now]:
  107. if vis[v] == i:
  108. flag = 0
  109. break
  110. if flag:
  111. dfs(now + 1)
  112. if ans:
  113. return
  114. dfs(0)
  115. global ans
  116. if ans == 0:
  117. tkinter.messagebox.showinfo(title='失败', message = '用' + str(m) + '种颜色染色失败!')
  118. else:
  119. maxx = 0
  120. for i in range(n):
  121. maxx = max(vis[i], maxx)
  122. tkinter.messagebox.showinfo(title='成功', message='共用' + str(maxx + 1) + '种颜色成功染色!')
  123. root.mainloop()
  124. move_down = 55
  125. l1 = tk.Label(window, text='图的结点数V', bg='green', font=('Arial', 12), width=20, height=2)
  126. l1.place(x = 0, y = 60 + move_down, anchor = 'nw')
  127. l2 = tk.Label(window, text='图的边数E', bg='green', font=('Arial', 12), width=20, height=2)
  128. l2.place(x = 0, y = 120 + move_down, anchor = 'nw')
  129. l3 = tk.Label(window, text = '染色最多需要的颜色数m', bg = 'green', font = ('Arial', 12), width = 20, height = 2)
  130. l3.place(x = 0, y = 180 + move_down, anchor = 'nw')
  131. e1 = tk.Entry(window, show = None)
  132. e1.place(x = 190, y = 70 + move_down, anchor = 'nw')
  133. l10 = tk.Label(window, text = ' <= 10', font = ('Arial', 12), width = 10, height = 2).place(x = 320, y = 60 + move_down, anchor = 'nw')
  134. e2 = tk.Entry(window, show = None)
  135. l11 = tk.Label(window, text = ' <= 20', font = ('Arial', 12), width = 10, height = 2).place(x = 320, y = 120 + move_down, anchor = 'nw')
  136. e2.place(x = 190, y = 130 + move_down, anchor = 'nw')
  137. l12 = tk.Label(window, text = ' <= 10', font = ('Arial', 12), width = 10, height = 2).place(x = 320, y = 180 + move_down, anchor = 'nw')
  138. e3 = tk.Entry(window, show = None)
  139. e3.place(x = 190, y = 190 + move_down, anchor = 'nw')
  140. b1 = tk.Button(window, text='确认', width=10,
  141. height=1, command=insert_point)
  142. b1.place(x = 150, y = 240 + move_down, anchor = 'nw')
  143. l4 = tk.Label(window, text='边的起点与终点', bg='green', font=('Arial', 12), width=20, height=2)
  144. l4.place(x=80, y=330 + move_down, anchor='nw')
  145. l5 = tk.Label(window, text='起点', bg='green', font=('Arial', 12), width=5, height=1)
  146. l5.place(x=0, y=385 + move_down, anchor='nw')
  147. l6 = tk.Label(window, text='终点', bg='green', font=('Arial', 12), width=5, height=1)
  148. l6.place(x=300, y=385 + move_down, anchor='nw')
  149. t = tk.Text(window, width=12, height=40, bg='yellow')
  150. t.place(x=560, y=150 + move_down, anchor='nw')
  151. l7 = tk.Label(window, text='已加入的边', bg='green', font=('Arial', 12), width=15, height=2)
  152. l7.place(x=530, y=70 + move_down, anchor='nw')
  153. l8 = tk.Label(window, text=' E未知')
  154. l8.place(x=565, y=120 + move_down, anchor='nw')
  155. e4 = tk.Entry(window, width=10, show=None)
  156. e4.place(x=0, y=435 + move_down, anchor='nw')
  157. e5 = tk.Entry(window, width=10, show=None)
  158. e5.place(x=300, y=435 + move_down, anchor='nw')
  159. b2 = tk.Button(window, text='确认', width=10, height=1, command = insert_point1)
  160. b2.place(x=150, y=480 + move_down, anchor='nw')
  161. l9 = tk.Label(window, text = '图染色问题可视化模拟', font = ('Arial', 20)).pack()
  162. window.mainloop()

2020.2.25 重置版

  1. import math
  2. import time
  3. import tkinter as tk
  4. import tkinter.messagebox
  5. from tkinter import *
  6. window = tk.Tk()
  7. window.title("着色问题")
  8. window.geometry("800x800")
  9. n = 0
  10. e = 0
  11. m = 0
  12. flag = 0
  13. ans = 0
  14. G = [[] for i in range(50)]
  15. color = ['red', 'green', 'blue' ,'yellow', 'brown','puruple', 'orange', 'grey', 'pink']
  16. xx0, yy0, xx1, yy1 = 55, 55, 120, 120
  17. x = []
  18. y = []
  19. x0 = []
  20. y0 = []
  21. x1 = []
  22. y1 = []
  23. k = 0
  24. vis = [-1 for i in range(20)]
  25. ans = 0
  26. cv = 0
  27. root = 0
  28. def add_edge(u, v):
  29. u -= 1
  30. v -= 1
  31. G[u].append(v)
  32. G[v].append(u)
  33. def init_vertex(): #这个函数是画结点.
  34. # x0, x1, y0, y1, x, y都是坐标.得先明白一下cv.create_oval(), cv.create_text(), cv.create_arc(), cv.create_line()这几个函数.
  35. # 见名知意.cv.create_oval()就是画椭圆(圆),前两个参数是左上角的横纵坐标,后两个参数是右下角的横纵坐标,最后一个fill=是颜色.如果我们使左上角,右下角围起来的是一个正方形,那么cv.create_oval()画出来的就是一个圆.
  36. # cv.create_text()就是放置文本.前两个参数就是文本的横纵坐标,最后一个text=字符串就是文本.
  37. # cv.create_arc()是画弓形(曲线),前四个参数和cv.create_oval()的意义相同.extent = 弧度制,就是你要画的那部分,180就是上半部分,-180就是下半部分.
  38. # cv.create_line()是画直线,前两个参数是一个端点的横纵坐标和另一个端点的横纵坐标.
  39. global cv
  40. for i in range(n):
  41. x0.append(xx0 + (i % k) * 100) # 每个圆中间间隔100.
  42. x1.append(xx1 + (i % k) * 100)
  43. y0.append(yy0 + (i >= k) * 100) #前一半的圆在上面,后一半的圆在下面.
  44. y1.append(yy1 + (i >= k) * 100)
  45. for i in range(n):
  46. x.append((x0[i] + x1[i]) / 2) #圆心坐标,标号用.
  47. y.append((y0[i] + y1[i]) / 2)
  48. print(x0, x1, y1)
  49. for i in range(n):
  50. cv.create_oval(x0[i], y0[i], x1[i], y1[i], fill = 'white') #画圆.
  51. cv.create_text(x[i], y[i], text = str(i + 1)) #标号.
  52. def init_edge(): # 这个函数是结点之间的连线.
  53. global cv
  54. f = 95
  55. for u in range(n):
  56. for v in G[u]:
  57. if u < k and v < k: # 都在上面的就拿弧的上半部分连线.
  58. cv.create_arc(x[u], y0[u] + f, x[v], y1[v] - f, extent=180, style=ARC)
  59. elif u >= k and v >= k: # 都在下面的就拿弧的下半部分连线.
  60. cv.create_arc(x[u], y0[u] + f, x[v], y1[v] - f, extent=-180, style=ARC)
  61. else: # 不在同一排就连直线.
  62. cv.create_line(x[u], y[u], x[v], y[v])
  63. for i in range(n): # 再画一遍圆,把圆内的线盖上.
  64. cv.create_oval(x0[i], y0[i], x1[i], y1[i], fill='white')
  65. cv.create_text(x[i], y[i], text=str(i + 1))
  66. def dfs(now):
  67. global ans, cv, root
  68. if now == n:
  69. ans = 1
  70. return
  71. for i in range(m):
  72. vis[now] = i
  73. cv.create_oval(x0[now], y0[now], x1[now], y1[now], fill = color[i]) #染色过程,给圆染上第i种颜色.
  74. cv.create_text(x[now], y[now], text = str(now + 1)) #再把号标上.
  75. root.update()
  76. time.sleep(0.5) #每一步停半秒.
  77. flag = 1
  78. for v in G[now]:
  79. if vis[v] == i:
  80. flag = 0
  81. break
  82. if flag:
  83. dfs(now + 1)
  84. if ans:
  85. return
  86. def colored():
  87. global k, cv, root
  88. root = tk.Tk() # 先创建一个窗口.
  89. root.geometry('600x600') # 设置窗口大小.
  90. cv = Canvas(root, width=600, height=600, bg='white') # 创建一个白色画布,用来在上面画画.
  91. cv.pack() # 把它放在窗口上.(没有side参数就默认放在窗口的正上部.)
  92. k = math.ceil(n / 2)
  93. init_vertex()
  94. init_edge()
  95. dfs(0)
  96. global ans
  97. if ans == 0:
  98. tkinter.messagebox.showinfo(title='失败', message='用' + str(m) + '种颜色染色失败!')
  99. else:
  100. maxx = 0
  101. for i in range(n):
  102. maxx = max(vis[i], maxx)
  103. tkinter.messagebox.showinfo(title='成功', message='共用' + str(maxx + 1) + '种颜色成功染色!')
  104. root.mainloop()
  105. def insert_point():
  106. global n, e, m, flag
  107. n = int(e1.get())
  108. e = int(e2.get())
  109. m = int(e3.get())
  110. if not(1 <= n <= 10 and 1 <= e <= 20 and 1 <= m <= 10):
  111. tkinter.messagebox.showwarning(title='警告', message='请输入正确的V,E,m。')
  112. e1.delete(0, 'end')
  113. e2.delete(0, 'end')
  114. e3.delete(0, 'end')
  115. return
  116. flag = 1
  117. l8 = tk.Label(window, text='(还剩余:' + str(e) + ')')
  118. l8.place(x=565, y=120 + move_down, anchor='nw')
  119. l13 = tk.Label(window, text=' <= ' + str(n), font=('Arial', 12), width=10, height=2).place(x=60, y=425 + move_down,
  120. anchor='nw')
  121. l14 = tk.Label(window, text=' <= ' + str(n), font=('Arial', 12), width=10, height=2).place(x=365, y=425 + move_down,
  122. anchor='nw')
  123. t.delete('1.0', 'end')
  124. def insert_point1():
  125. global n, e, m
  126. if not n and not e and not m:
  127. tkinter.messagebox.showwarning(title='警告', message='请先输入V, E, m。')
  128. return
  129. u = int(e4.get())
  130. v = int(e5.get())
  131. e4.delete(0, 'end')
  132. e5.delete(0, 'end')
  133. if not(1 <= u <= n and 1 <= v <= n):
  134. tkinter.messagebox.showwarning(title='警告', message='请先输入正确的起点和终点。')
  135. return
  136. if (v - 1) in G[u - 1]:
  137. tkinter.messagebox.showwarning(title='警告', message='这条边已经被输入过了。')
  138. return
  139. e -= 1
  140. add_edge(u, v)
  141. l8 = tk.Label(window, text='(还剩余:' + str(e) + ')').place(x=565, y=120 + move_down, anchor='nw')
  142. t.insert('insert', ' ' + str(u) + '---' + str(v) + '\n')
  143. if e == 0:
  144. colored()
  145. move_down = 55
  146. l1 = tk.Label(window, text='图的结点数V', bg='green', font=('Arial', 12), width=20, height=2)
  147. l1.place(x = 0, y = 60 + move_down, anchor = 'nw')
  148. l2 = tk.Label(window, text='图的边数E', bg='green', font=('Arial', 12), width=20, height=2)
  149. l2.place(x = 0, y = 120 + move_down, anchor = 'nw')
  150. l3 = tk.Label(window, text = '染色最多需要的颜色数m', bg = 'green', font = ('Arial', 12), width = 20, height = 2)
  151. l3.place(x = 0, y = 180 + move_down, anchor = 'nw')
  152. e1 = tk.Entry(window, show = None)
  153. e1.place(x = 190, y = 70 + move_down, anchor = 'nw')
  154. l10 = tk.Label(window, text = ' <= 10', font = ('Arial', 12), width = 10, height = 2).place(x = 320, y = 60 + move_down, anchor = 'nw')
  155. e2 = tk.Entry(window, show = None)
  156. l11 = tk.Label(window, text = ' <= 20', font = ('Arial', 12), width = 10, height = 2).place(x = 320, y = 120 + move_down, anchor = 'nw')
  157. e2.place(x = 190, y = 130 + move_down, anchor = 'nw')
  158. l12 = tk.Label(window, text = ' <= 10', font = ('Arial', 12), width = 10, height = 2).place(x = 320, y = 180 + move_down, anchor = 'nw')
  159. e3 = tk.Entry(window, show = None)
  160. e3.place(x = 190, y = 190 + move_down, anchor = 'nw')
  161. b1 = tk.Button(window, text='确认', width=10,
  162. height=1, command=insert_point)
  163. b1.place(x = 150, y = 240 + move_down, anchor = 'nw')
  164. l4 = tk.Label(window, text='边的起点与终点', bg='green', font=('Arial', 12), width=20, height=2)
  165. l4.place(x=80, y=330 + move_down, anchor='nw')
  166. l5 = tk.Label(window, text='起点', bg='green', font=('Arial', 12), width=5, height=1)
  167. l5.place(x=0, y=385 + move_down, anchor='nw')
  168. l6 = tk.Label(window, text='终点', bg='green', font=('Arial', 12), width=5, height=1)
  169. l6.place(x=300, y=385 + move_down, anchor='nw')
  170. t = tk.Text(window, width=12, height=40, bg='yellow')
  171. t.place(x=560, y=150 + move_down, anchor='nw')
  172. l7 = tk.Label(window, text='已加入的边', bg='green', font=('Arial', 12), width=15, height=2)
  173. l7.place(x=530, y=70 + move_down, anchor='nw')
  174. l8 = tk.Label(window, text=' E未知')
  175. l8.place(x=565, y=120 + move_down, anchor='nw')
  176. e4 = tk.Entry(window, width=10, show=None)
  177. e4.place(x=0, y=435 + move_down, anchor='nw')
  178. e5 = tk.Entry(window, width=10, show=None)
  179. e5.place(x=300, y=435 + move_down, anchor='nw')
  180. b2 = tk.Button(window, text='确认', width=10, height=1, command = insert_point1)
  181. b2.place(x=150, y=480 + move_down, anchor='nw')
  182. l9 = tk.Label(window, text = '图染色问题可视化模拟', font = ('Arial', 20)).pack()
  183. window.mainloop()

2020.2.25 染色过程 注释版

  1. import math
  2. import time
  3. import tkinter as tk #用python中的tkinter模块和其中的Canvas画布控件.
  4. from tkinter import *
  5. # 用dfs深度搜索来解决这个问题.
  6. color = ['red', 'green', 'blue' ,'yellow', 'brown','puruple', 'orange', 'grey', 'pink']
  7. root = tk.Tk() # 先创建一个窗口.
  8. root.geometry('600x600') #设置窗口大小.
  9. cv = Canvas(root,width = 600, height = 600, bg = 'white') # 创建一个白色画布,用来在上面画画.
  10. cv.pack() # 把它放在窗口上.(没有side参数就默认放在窗口的正上部.)
  11. n = 4
  12. m = 4
  13. xx0, yy0, xx1, yy1 = 55, 55, 120, 120 # 画圆的起始坐标,第一个圆的左上角和右下角.
  14. x = []
  15. y = []
  16. x0 = []
  17. y0 = []
  18. x1 = []
  19. y1 = []
  20. k = math.ceil(n / 2) #ceil()是向上取整函数,例如ceil(2.5) = 3, ceil(2.01) = 3, ceil(2) = 2.
  21. G = [[] for i in range(50)] # 这个就是c++中的vector<int> G[50],用来存图的.
  22. def add_edge(u, v):
  23. u -= 1 #u--,v-- 是因为输入的是1 -- 2,但我们存下来的实际是0 -- 1.
  24. v -= 1
  25. G[u].append(v) # 无向边,正着来一边,反着来一遍.
  26. G[v].append(u)
  27. def init_vertex(): #这个函数是画结点.
  28. # x0, x1, y0, y1, x, y都是坐标.得先明白一下cv.create_oval(), cv.create_text(), cv.create_arc(), cv.create_line()这几个函数.
  29. # 见名知意.cv.create_oval()就是画椭圆(圆),前两个参数是左上角的横纵坐标,后两个参数是右下角的横纵坐标,最后一个fill=是颜色.如果我们使左上角,右下角围起来的是一个正方形,那么cv.create_oval()画出来的就是一个圆.
  30. # cv.create_text()就是放置文本.前两个参数就是文本的横纵坐标,最后一个text=字符串就是文本.
  31. # cv.create_arc()是画弓形(曲线),前四个参数和cv.create_oval()的意义相同.extent = 弧度制,就是你要画的那部分,180就是上半部分,-180就是下半部分.
  32. # cv.create_line()是画直线,前两个参数是一个端点的横纵坐标和另一个端点的横纵坐标.
  33. for i in range(n):
  34. x0.append(xx0 + (i % k) * 100) # 每个圆中间间隔100.
  35. x1.append(xx1 + (i % k) * 100)
  36. y0.append(yy0 + (i >= k) * 100) #前一半的圆在上面,后一半的圆在下面.
  37. y1.append(yy1 + (i >= k) * 100)
  38. for i in range(n):
  39. x.append((x0[i] + x1[i]) / 2) #圆心坐标,标号用.
  40. y.append((y0[i] + y1[i]) / 2)
  41. for i in range(n):
  42. cv.create_oval(x0[i], y0[i], x1[i], y1[i], fill = 'white') #画圆.
  43. cv.create_text(x[i], y[i], text = str(i + 1)) #标号.
  44. def init_edge(): # 这个函数是结点之间的连线.
  45. f = 95
  46. for u in range(n):
  47. for v in G[u]:
  48. if u < k and v < k: # 都在上面的就拿弧的上半部分连线.
  49. cv.create_arc(x[u], y0[u] + f, x[v], y1[v] - f, extent = 180, style = ARC)
  50. elif u >= k and v >= k: # 都在下面的就拿弧的下半部分连线.
  51. cv.create_arc(x[u], y0[u] + f, x[v], y1[v] - f, extent = -180, style = ARC)
  52. else: # 不在同一排就连直线.
  53. cv.create_line(x[u], y[u], x[v], y[v])
  54. for i in range(n): #再画一遍圆,把圆内的线盖上.
  55. cv.create_oval(x0[i], y0[i], x1[i], y1[i], fill = 'white')
  56. cv.create_text(x[i], y[i], text = str(i + 1))
  57. vis = [-1 for i in range(20)]
  58. ans = 0
  59. def dfs(now):
  60. global ans
  61. if now == n:
  62. ans = 1
  63. return
  64. for i in range(m):
  65. vis[now] = i
  66. cv.create_oval(x0[now], y0[now], x1[now], y1[now], fill = color[i]) #染色过程,给圆染上第i种颜色.
  67. cv.create_text(x[now], y[now], text = str(now + 1)) #再把号标上.
  68. root.update()
  69. time.sleep(0.5) #每一步停半秒.
  70. flag = 1
  71. for v in G[now]:
  72. if vis[v] == i:
  73. flag = 0
  74. break
  75. if flag:
  76. dfs(now + 1)
  77. if ans:
  78. return
  79. add_edge(1, 2)
  80. add_edge(1, 3)
  81. add_edge(1, 4)
  82. add_edge(2, 4)
  83. init_vertex()
  84. init_edge()
  85. dfs(0)
  86. root.mainloop()

老板,还有什么要加上的吗?

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注