Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 记忆和路径数组_Algorithm_Path_Sum_Memoization - Fatal编程技术网

Algorithm 记忆和路径数组

Algorithm 记忆和路径数组,algorithm,path,sum,memoization,Algorithm,Path,Sum,Memoization,我的问题是,我设法递归地解决,但我希望有一个更优化的解决方案,使用记忆,但我没有足够的技能知道它在这种情况下应该如何工作 问题是: 有一个二维数组,其中包含随机的正整数,您将获得随机的移动量。从左上角开始移动,只允许进行以下移动: 左边 正确的 向下 您需要在最后一排结束,并在途中收集尽可能多的信息。你不能重新考虑一个职位 记忆是关于存储价值并在此基础上构建未来的结果,但既然有这么多的路径可以选择,我该如何使用它?我应该用的是回忆录,还是我猜错了:) 我想对问题的描述是不对的。起点是左上角,唯一

我的问题是,我设法递归地解决,但我希望有一个更优化的解决方案,使用记忆,但我没有足够的技能知道它在这种情况下应该如何工作

问题是:

有一个二维数组,其中包含随机的正整数,您将获得随机的移动量。从左上角开始移动,只允许进行以下移动: 左边 正确的 向下

您需要在最后一排结束,并在途中收集尽可能多的信息。你不能重新考虑一个职位


记忆是关于存储价值并在此基础上构建未来的结果,但既然有这么多的路径可以选择,我该如何使用它?我应该用的是回忆录,还是我猜错了:)

我想对问题的描述是不对的。起点是左上角,唯一允许的移动是:右下。如果这样,也可以进行记忆,您可以使用0表示右,1表示下,然后每个路径都可以用二进制字符串表示,然后您可以使用数组dp[int(string)]来记忆值。

这基本上是一个动态编程问题。你不必考虑每一步的所有可能的路径,因为路径的细节不会影响未来的决定。对于每个单元格,您需要知道如果您朝特定方向移动并移动特定次数,可以收集的最大数量。没有什么能影响你的决定

def bestAmount(x,y,direction,n_moves):
  # If this position is off the grid, then this isn't a valid state
  if x<0 or x>=width or y>=height:
    return None
  if n_moves==0:
    # We came to the last move.
    if y!=height-1:
      # If we're not at the bottom row then this isn't a valid path.
      return None
    # Otherwise, the final cell value is part of our total.
    return cellValue(x,y)
  if direction=='down':
    # If we came down to get to this position, then the next move
    # can be either  left, right, or down
    left = bestAmount(x-1,y,'left',n_moves-1)
    right = bestAmount(x+1,y,'right',n_moves-1)
    down = bestAmount(x,y+1,'down',n_moves-1)
    return max(left,right,down)+cellValue(x,y)
  if direction=='left':
    # If we moved left to get to this position, then
    # we can't go right, since we would be visiting the same position again.
    left = bestAmount(x-1,y,'left',n_moves-1)
    down = bestAmount(x,y+1,'down',n_moves-1)
    return max(left,down)+cellValue(x,y)
  if direction=='right':
    # same logic as for left, but opposite.
    right = bestAmount(x+1,y,'right',n_moves-1)
    down = bestAmount(x,y+1,'down',n_moves-1)
    return max(right,down)+cellValue(x,y)

def solve(n_moves):
  # Start by pretending we entered the starting cell by going down
  return bestAmount(0,0,'down',n_moves)
def最佳数量(x、y、方向、n_移动):
#如果此位置脱离网格,则此状态无效
如果x=宽度或y>=高度:
一无所获
如果n_moves==0:
#我们走到最后一步。
如果你是我的话=高度-1:
#如果我们不在最后一行,那么这不是一个有效的路径。
一无所获
#否则,最终的单元格值是总数的一部分。
返回单元格值(x,y)
如果方向==“向下”:
#如果我们下来是为了达到这个位置,那么下一步
#可以是左、右或下
左=最佳金额(x-1,y,'left',n_-1)
右=最佳金额(x+1,y,'right',n_-1)
向下=最佳金额(x,y+1,'down',n_-1)
返回最大值(左、右、下)+单元格值(x、y)
如果方向==“左”:
#如果我们向左移动到这个位置,那么
#我们不能向右走,因为我们将再次访问同一位置。
左=最佳金额(x-1,y,'left',n_-1)
向下=最佳金额(x,y+1,'down',n_-1)
返回最大值(左,下)+单元格值(x,y)
如果方向==‘正确’:
#逻辑与左相同,但相反。
右=最佳金额(x+1,y,'right',n_-1)
向下=最佳金额(x,y+1,'down',n_-1)
返回最大值(右下)+单元格值(x,y)
def解算(n_移动):
#假装我们进入了起始牢房
返回最佳金额(0,0,'down',n_移动)
如果记忆bestAmount,那么您将得到一个相当有效的解决方案,因为可能性的数量相对有限


不过,将其视为动态规划问题将为您提供更有效的解决方案。

@Radu:tag[algorithm]意味着,只要描述了算法,确切的语言对OP并不重要。什么是没有意义的?:)还有,谢谢你,沃恩,这正是我需要的!:)@用户1577191:实际上,从头开始工作更好