如何计算船上两点的杀虫剂距离?

时间:2019-03-20 21:27:43

标签: python-3.x euclidean-distance

我有这个代码。它计算板上的点的距离,并输出成对的距离等于或小于k的点。但是,对于较大的数量,它非常慢(大约为O(1,5(n ^ 2)))。那么,您能建议如何使它更有效吗?

import numpy
input = open("02.in").read()
input = input.split("\n")
file = open("output2.txt", "w+")

data = []
output = []

for i in input:
    data.append(i.split())
#so data looks like [["100000","10"],["235","654"],["4538","1535"],["965","3412"],...] 
#and in first list is info about rest (number of lines and constant k)
k = int(data[0][1])
length = int(data[0][0])
print(start)
print(length)

data.pop(-1)
data.pop(0)

for i in range(length):
    x1, y1 = int(data[i][0]), int(data[i][1])
    for j in range(i + 1, length):
        x, y = int(data[j][0]) - x1, int(data[j][1]) - y1
        if abs(x) > k or abs(y) > k:
            continue
        dist = numpy.linalg.norm(x-y)
        if(dist <= k):
            print(str(i) + " " + str(j))
            file.write(str(i) + " " + str(j) + "\n")

file.close()

如果您有任何问题,请问我

0 个答案:

没有答案