达到结束动态程序迁移的最小跳转次数

时间:2019-10-20 23:38:34

标签: dynamic dynamic-programming

给出一个数组,从第一个元素开始验证到达终点需要多少步骤。

例如:arr = [1、3、5、8、4、2、6、7、0、7、9]

1-> 3-> 8(这是最短的路径)

3个步骤。

到目前为止,我从极客那里得到了这段代码:

def jumpCount(x, n): 
  jumps = [0 for i in range(n)] 

  if (n == 0) or (x[0] == 0): 
      return float('inf') 

  jumps[0] = 0


  for i in range(1, n): 
      jumps[i] = float('inf')   
      for j in range(i): 
          if (i <= j + x[j]) and (jumps[j] != float('inf')): 
              jumps[i] = min(jumps[i], jumps[j] + 1) 
              break                 
  return jumps[n-1]     

def jumps(x):
  n = len(x)
  return jumpCount(x,n) 

x = [1, 3, 5, 8, 4, 2, 6, 7, 0, 7, 9]

print(jumps(x))

但是我想打印出最短路径(1-3-8)的数字。我该如何修改代码来做到这一点?

我尝试创建j的列表,但是由于5在循环中经过测试,因此也将其追加。

链接到问题: https://www.geeksforgeeks.org/minimum-number-of-jumps-to-reach-end-of-a-given-array/

1 个答案:

答案 0 :(得分:0)

基本思想是,您需要一个辅助结构来帮助您跟踪最小路径。这些类型的结构通常称为“ backpointers”(在我们的案例中,您可以将它们称为“ forwardpointers”,因为我们正在前进,,)。我的代码以递归方式解决了该问题,但是可以迭代地完成。该策略如下:

jumps_vector = [ 1, 3, 5, 8, 4, 2, 6, 7, 0, 7, 9 ]

"""
fwdpointers holds the relative jump size to reach the minimum number of jumps
for every component of the original vector
"""
fwdpointers = {}

def jumps( start ):
    if start == len( jumps_vector ) - 1:
        # Reached the end
        return 0
    if start > len( jumps_vector ) - 1:
        # Cannot go through that path
        return math.inf
    if jumps_vector[ start ] == 0:
        # Cannot go through that path (infinite loop with itself)
        return math.inf

    # Get the minimum in a traditional way
    current_min = jumps( start + 1 )
    fwdpointers[ start ] = start + 1
    for i in range( 2, jumps_vector[ start ] + 1 ):
        aux_min = jumps( start + i )
        if current_min > aux_min:
            # Better path. Update minimum and fwdpointers
            current_min = aux_min
            # Store the (relative!) index of where I jump to
            fwdpointers[ start ] = i

    return 1 + current_min

在这种情况下,变量fwdpointers存储我跳转到的位置的相对索引。例如fwdpointers[ 0 ] = 1,因为我将跳到相邻的数字,而fwdpointers[ 1 ] = 2因为我将跳到下一数字。

这样做,那么只需对main()函数进行一些后处理即可。

if __name__ == "__main__":
    min_jumps = jumps( 0 )
    print( min_jumps )

    # Holds the index of the jump given such that
    # the sequence of jumps are the minimum
    i = 0
    # Remember that the contents of fwdpointers[ i ] are the relative indexes
    # of the jump, not the absolute ones
    print( fwdpointers[ 0 ] )
    while i in fwdpointers and i + fwdpointers[ i ] < len( jumps_vector ):
        print( jumps_vector[ i + fwdpointers[ i ] ] )
        # Get the index of where I jump to
        i += fwdpointers[ i ]
        jumped_to = jumps_vector[ i ]

我希望这能回答您的问题。


编辑:我认为迭代版本更具可读性:

results = {}
backpointers = {}
def jumps_iter():
    results[ 0 ] = 0
    backpointers[ 0 ] = -1
    for i in range( len( jumps_vector ) ):
        for j in range( 1, jumps_vector[ i ] + 1 ):
            if ( i + j ) in results:
                results[ i + j ] = min( results[ i ] + 1, results[ i + j ] )
                if results[ i + j ] == results[ i ] + 1:
                    # Update where I come from
                    backpointers[ i + j ] = i
            elif i + j < len( jumps_vector ):
                results[ i + j ] = results[ i ] + 1
                # Set where I come from
                backpointers[ i + j ] = i

    return results[ len( jumps_vector ) - 1 ]

以及后处理:

i = len( jumps_vector ) - 1
print( jumps_vector[ len( jumps_vector ) - 1 ], end = " " )
while backpointers[ i ] >= 0:
    print( jumps_vector[ backpointers[ i ] ], end = " " )
    i = backpointers[ i ]
print()