Login
A   A   A  
welcome

 Album 

米Teen唔鐘意玩現成品,喜歡由零件開始砌。99次失敗唔係問題,最緊要係有1次成功!

米Teen總部設在聖公會白約翰會督中學506室,在午膳後和放學後集會。有興趣參與的同學,請與會長或導師聯絡。

米Teen專頁

街景
2025-08-08 22:49:36
Rm506 溫度31℃ 濕度90%
2025-08-07 11:29:14
LIGHT Noise 0 0


2025-08-07 13:53:08
LIGHT Disturber 0 0


2025-08-07 13:53:47
LIGHT Disturber 0 0


2025-08-08 10:49:02
LIGHT Lightning 31 0


2025-08-08 10:53:12
LIGHT Disturber 0 0


2025-08-08 11:01:14
LIGHT Disturber 0 0


2025-08-08 12:14:42
LIGHT Lightning 14 2


2025-08-08 14:03:06
LIGHT Disturber 0 0


2025-08-08 17:32:12
LIGHT Disturber 0 0


2025-08-08 17:32:13
LIGHT Disturber 0 0


Connect all cities.

icon

Home>ICT>Python by ...>Connect al...

Find the shortest route to connect all the cities.

1# Author: HUANG KWUN HOI (SKHBBSS, class 4D)
2# Last modified: 2020.9.27
3draw=True
4import math
5import tkinter as tk
6filename=input('Enter the data file name:')
7print('Hold down #Enter for some time')
8f=open(filename,'r')
9tk.TkVersion
10top=tk.Tk()
11top.title('Route')
12C=tk.Canvas(top,bg='white',width=1820,height=920)
13xy_list=[]
14station=[]
15def cal(x1,x2,y1,y2):
16 x_diff=float(x1-x2)
17 y_diff=float(y1-y2)
18 result=abs(math.sqrt(x_diff**2+y_diff**2))
19 return result
20for data in f:
21 a=data
22 b=a.split(',')
23 xy_list.append([int(b[1]),int(b[2])])
24f.close()
25qq=len(xy_list)
26station.clear()
27station.append(xy_list[0])
28shortest_distance=0
29for q in range(qq-1):
30 shortest=999999
31 for w in station:
32 for e in xy_list:
33 ww=cal(e[0],w[0],e[1],w[1])
34 if e not in station and ww<shortest:
35 shortest=ww
36 ee=w
37 rr=e
38 station.append(rr)
39 shortest_distance+=cal(ee[0],rr[0],ee[1],rr[1])
40 if draw:
41 C.create_line(ee[0],ee[1],rr[0],rr[1])
42 C.pack()
43 pause=input()
44print('Total length: ',shortest_distance)
45top.mainloop()

Download source code

Download data file

welcome

 Album 

米Teen唔鐘意玩現成品,喜歡由零件開始砌。99次失敗唔係問題,最緊要係有1次成功!

米Teen總部設在聖公會白約翰會督中學506室,在午膳後和放學後集會。有興趣參與的同學,請與會長或導師聯絡。

米Teen專頁

街景
2025-08-08 22:49:36
Rm506 溫度31℃ 濕度90%
2025-08-07 11:29:14
LIGHT Noise 0 0


2025-08-07 13:53:08
LIGHT Disturber 0 0


2025-08-07 13:53:47
LIGHT Disturber 0 0


2025-08-08 10:49:02
LIGHT Lightning 31 0


2025-08-08 10:53:12
LIGHT Disturber 0 0


2025-08-08 11:01:14
LIGHT Disturber 0 0


2025-08-08 12:14:42
LIGHT Lightning 14 2


2025-08-08 14:03:06
LIGHT Disturber 0 0


2025-08-08 17:32:12
LIGHT Disturber 0 0


2025-08-08 17:32:13
LIGHT Disturber 0 0


 ⇧