一只青蛙想过河。
河里有3块石头她可以跳过去。
她想在所有可能的路径中选择导致最小最长跳跃的路径。
IE。每条可能的路径都会有一个最长的跳跃。她需要找到最长跳跃最小的路径。
2 个海岸相距 10 并且平行于 y 轴。
每个石头位置由 x 位置的列表 x=[x1,x2,x3] 和 y 位置的列表 y=[y1,y2,y3] 给出。
通过路径中石头列表 x 和 y 中的索引列表返回此路径中最长的跳跃(四舍五入到最接近的整数)和路径本身。
这是我找到最长跳跃的python代码。
我将如何跟踪路径本身?
而且我的代码看起来很笨拙,有 3 个嵌套循环有没有更好/更优雅的方式来编写这段代码?
def longestJump(x, y):
best = 10
for i in range(0,3):
for j in range(0,3):
for k in range(0,3):
# first jump from shore to a stone
dist = x[i]
# second jump between stones
dist = max(dist, round(math.sqrt((x[i]-x[j])**2 + (y[i]-y[j])**2)))
# third jump between stones
dist = max(dist, round(math.sqrt((x[i]-x[k])**2 + (y[i]-y[k])**2)))
dist = max(dist, round(math.sqrt((x[j]-x[k])**2 + (y[j]-y[k])**2)))
# last jump from a stone to the opposite shore
dist = max(dist, 10 - x[j])
best = min(dist, best)
return best