top of page

from math import *
x=[0, 54, -15, -23, 47, 12, 28, 30, 14, -88, 2, 92, -6, 13,-27]
y=[0, 41, 99, -40, -20, 0, -99, -5, 14, -98, 16, 97, 55, 43, 3]
distances=[0]
maisons=[0]

def distance_entre_deux_points(xa,xb,ya,yb):
   return sqrt((xb-xa)**2+(ya-yb)**2)

for j in range(1,len(x)):

   dist_inter=[]
   for i in range(1,len(x)):
       if i not in maisons:
           dist_inter.append(distance_entre_deux_points(x[0],x[i],y[0],y[i]))
       else:
           dist_inter= maisons(10000)
distances.append(min(dist_inter))
maisons.append(dist_inter.index(min(dist_inter)))
print(maisons)

Algorithme glouton:

bottom of page