简体   繁体   English

如何找到最长的最小路径?

[英]How to find the longest smallest path?

A frog wants to cross a river. 青蛙想过河。

There are 3 stones in the river she can jump to. 她可以跳到河里的三块石头上。

She wants to choose among all possible paths the one that leads to the smallest longest jump. 她想在所有可能的路径中选择一条导致最小的最长跳动的路径。

Ie. 就是 each of the possible paths will have one jump that is the longest. 每个可能的路径都会有一个最长的跳跃。 She needs to find the path where this longest jump is smallest. 她需要找到最长的跳跃最小的路径。

The 2 shores are 10 apart and are parallel to the y axis. 2个海岸相距10,且与y轴平行。

Each stone position is given by a list x=[x1,x2,x3] of the x positions and y=[y1,y2,y3] of the y positions. 每个石头位置由x位置的列表x = [x1,x2,x3]和y = [y1,y2,y3]给出。

Return both the longest jump in this path (rounded to the closest integer) and the path itself through a list of indices in the lists x and y of the stones in the path. 通过路径中石头的列表x和y中的索引列表,返回该路径中最长的跳转(四舍五入到最接近的整数)和路径本身。

Here it is my python code to find the longest jump. 这是我找到最长跳的python代码。

How would I track the path itself? 我将如何跟踪路径本身?

And my code looks clumsy with 3 nested loops is there a better/more elegant way to write this code? 而且我的代码看起来很笨拙,带有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

You don't need to take the square-root except for the final result. 除了最终结果外,您不需要取平方根。 Just compute "distance squared" and compare with that. 只需计算“距离平方”并与之比较即可。

Not sure what you are calling "round" either. 也不确定您所说的“回合”是什么。 That would potentially create a bug. 那可能会产生一个错误。

Also, don't you need to skip all cases of "i == j" from the inner loop? 另外,您是否不需要从内部循环中跳过所有“ i == j”的情况?

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, (x[i]-x[j])**2 + (y[i]-y[j])**2)
                   # third jump between stones
                   dist = max(dist, (x[i]-x[k])**2 + (y[i]-y[k])**2) 
                   dist = max(dist, 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 math.sqrt(best)

You can simplify the three loops by using itertools.permutations on a range . 您可以通过在range上使用itertools.permutations来简化三个循环。 That will return a three-tuple if you pass it a range of length 3. 如果将长度range为3,则将返回一个三元组。

As for keeping track of the path, I think it would be much easier if you used an actual if statement to compare the largest jump lengths from each path to the best you've seen so far, rather than using min . 至于跟踪路径,我认为如果使用实际的if语句比较从每个路径到迄今为止所见的最佳路径的最大跳转长度,而不是使用min ,会容易得多。 In an if , you can also save additional information (such as the path that has the smallest largest jump) at the same time you're saving that jump's length. if ,您还可以在保存该跳转的长度的同时保存其他信息(例如,具有最小跳转的路径)。

def longestJump(x, y):
    best_jump = 10 # infinity
    best_path = ()
    for i, j, k in itertools.permutations(range(3)):           
        jump0 = x[i]                                      # shore to i
        jump1 = sqrt((x[i]-x[j])**2 + (y[i]-y[j])**2)     # i to j
        jump2 = sqrt((x[j]-x[k])**2 + (y[i]-y[j])**2)     # j to k
        jump3 = 10 - x[k]                                 # k to far shore
        longest = max(jump0, jump1, jump2, jump3)
        if longest < best_jump:
            best_jump = longest
            best_path = (i, j, k)
    return best_jump, best_path

This always expects the path to use all three stones. 始终希望这条路径可以使用所有这三个石头。 If that's not required, you may want to iterate over permutations of the each subset of the stones. 如果这不是必需的,则可能要遍历宝石的每个子集的排列。 I'm not sure if there's a particularly easy way to do that, but you could try combining itertools.combinations and the permutations code above. 我不确定是否有一种特别简单的方法来执行此操作,但是您可以尝试将itertools.combinations和上面的permutations代码结合起来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM