Geometry 出租车几何任务
所以,我为这个任务奋斗了一段时间。听起来是这样的:给定N个点(X,Y)X,Y个整数,以及形式为P(A,B)的M个问题,求出从点P(A,B)到所有N个给定点的总距离。从A(x1,y1)到B(x2,y2)的距离=最大值(| x1-x2 |,| y1-y2 |)。也许听起来很奇怪,我不是一个说英语的人,为我的错误感到抱歉。我将把钥匙留在这里Geometry 出租车几何任务,geometry,distance,computational-geometry,Geometry,Distance,Computational Geometry,所以,我为这个任务奋斗了一段时间。听起来是这样的:给定N个点(X,Y)X,Y个整数,以及形式为P(A,B)的M个问题,求出从点P(A,B)到所有N个给定点的总距离。从A(x1,y1)到B(x2,y2)的距离=最大值(| x1-x2 |,| y1-y2 |)。也许听起来很奇怪,我不是一个说英语的人,为我的错误感到抱歉。我将把钥匙留在这里 IN.txt(N=4,M=3,前4个坐标表示给定的点。 接下来的3个坐标是我必须计算总长度的点) 4 3 3 5 -3 -2 1 4 -4 -3 2 -4
IN.txt(N=4,M=3,前4个坐标表示给定的点。
接下来的3个坐标是我必须计算总长度的点)
4 3
3 5
-3 -2
1 4
-4 -3
2 -4
1 4
4 2
OUT.txt
28
15
21
这里有一些Python可以帮你解决这个问题。写东西时一定要注意你所在的目录,这样你就不会覆盖内容
我已经用您在问题中提供的输入信息对它进行了测试,它工作正常,提供了所需的格式化输出文件
# Assuming you're in the directory to IN.txt -- otherwise, insert the filepath.
input_file = open("IN.txt", "r")
# Read the input file and split it by new lines
input_lines_raw = input_file.read().split('\n')
input_file.close()
# Split the input lines and eliminate the spaces/create the vector int lists
input_lines_split = []
for element in input_lines_raw:
input_lines_split.append(element.split(' '))
input_lines = []
for sub in input_lines_split:
inserter = []
for elem in sub:
if (len(elem) > 0):
inserter.append(elem)
input_lines.append(inserter)
input_lines = [[int(j) for j in i] for i in input_lines]
# Build the original and final vector arrays
origin_vectors = []
dest_vectors = []
for i in range(1, input_lines[0][0] + 1):
dest_vectors.append(input_lines[i])
for i in range(input_lines[0][0] + 1, input_lines[0][0] + input_lines[0][1] + 1):
origin_vectors.append(input_lines[i])
# "Distance" operations on the lists of vectors themselves/generate results array
results_arr = []
for original in origin_vectors:
counter = 0
for final in dest_vectors:
counter = counter + max(abs(original[0] - final[0]), abs(original[1] - final[1]))
results_arr.append(counter)
print(results_arr)
for element in results_arr:
print(str(element))
# Open the ouput file and write to it, creating a new one if it doesn't exist.
# NOTE: This will overrwrite any existing "OUT.txt" file in the current directory.
output_file = open("OUT.txt", "w")
for element in results_arr:
output_file.write(str(element) + '\n')
output_file.close()