我最长的ruby算法有什么问题?

我最长的ruby算法有什么问题?,ruby,algorithm,matrix,Ruby,Algorithm,Matrix,使用Ruby语言,让函数LongestMatrixPath(strArr)获取存储在strArr中的字符串数组,该数组将是一个由正一位数整数组成的NxM矩阵,并找到由不同整数组成的最长递增路径。在矩阵中移动时,只能向上、向下、向左和向右移动。例如:如果strArr为[“345”、“326”、“221”],则该矩阵如下所示: 3 4 5 3 2 6 2 2 1 对于上述输入,最长的增加路径为:3->4->5->6。您的程序应该返回最长路径中的连接数,因此对于此输入,您的程序应该返回3。矩阵中不一定

使用Ruby语言,让函数LongestMatrixPath(strArr)获取存储在strArr中的字符串数组,该数组将是一个由正一位数整数组成的NxM矩阵,并找到由不同整数组成的最长递增路径。在矩阵中移动时,只能向上、向下、向左和向右移动。例如:如果strArr为[“345”、“326”、“221”],则该矩阵如下所示:

3 4 5

3 2 6

2 2 1

对于上述输入,最长的增加路径为:3->4->5->6。您的程序应该返回最长路径中的连接数,因此对于此输入,您的程序应该返回3。矩阵中不一定总是有最长的路径

    def recursiveFunction(array, row, col, path)
      current = array[row][col]
      row-1 >= 0  ? up = array[row-1][col] : up = nil 
      row+1 < array.length ? down = array[row+1][col] : down = nil 
      col-1 >= 0 ? left = array[row][col-1] : left = nil 
      col+1 < array[row].length ? right = array[row][col+1] : right = 
      nil 

      if !path.include?(current) && current.to_i > path[-1].to_i 
        path.push(current) 
        p path 
        if up && up.to_i > current.to_i 
          recursiveFunction(array, (row-1), col, path)
        elsif down && down.to_i > current.to_i 
          recursiveFunction(array, (row+1), col, path)
        elsif left && left.to_i > current.to_i 
          recursiveFunction(array, row, (col-1), path)
        elsif right && right.to_i > current.to_i 
          recursiveFunction(array, row, (col+1), path)
        end
      end 

      path 
    end

    def LongestMatrixPath(strArr)
      # turn each string into an array
      strArr.map!{|str| str.split("")}

      # calculate up, down, left, right formula
      # up = [-1][same]
      # down = [+2][same]
      # left = [same][-1] 
      # right = [same][+1]

      # create data structure to store paths
      path = []

      # create loop that loops through each element in each array 
      inside strArr
      # then apply recursive function to each element
      longest_path = 0
      row = 0
      while row < strArr.length 
        col = 0 
        while col < strArr[row].length  
          result = recursiveFunction(strArr, row, col, path=[])
          longest_path = result.length if result.length > longest_path 
          col += 1
        end
        row += 1 
      end

      # return number of connections 
      result.length-1    
    end


    # LongestMatrixPath(["345", "326", "221"]) 
    # correct answer: 3, my answer: 3

    # LongestMatrixPath(["12256", "56219", "43215"])
    # correct answer: 4, my answer: 3

    # LongestMatrixPath(["67", "21", "45"]) 
    # correct answer: 3, my answer: 1

    # LongestMatrixPath(["111", "111", "111"]) 
    # correct answer: 0, my answer: 0

    # LongestMatrixPath(["123", "456", "789"]) 
    # correct answer: 4, my answer: 4
def recursiveFunction(数组、行、列、路径)
当前=数组[行][列]
第1行>=0?up=数组[第1行][col]:up=nil
行+1=0?左=数组[行][col-1]:左=零
列+1<数组[行]。长度?右=数组[行][col+1]:右=
无
如果!路径。包括?(当前)和¤t.to_i>路径[-1]。to_i
路径推送(当前)
p路径
如果向上(&up.to_i>current.to_i
递归函数(数组,(第1行),列,路径)
elsif down&&down.to_i>current.to_i
递归函数(数组,(行+1),列,路径)
elsif left&&left.to_i>current.to_i
递归函数(数组,行,(列-1),路径)
elsif right&&right.to_i>current.to_i
递归函数(数组,行,(列+1),路径)
结束
结束
路径
结束
def LongestMatrixPath(strArr)
#将每个字符串转换为一个数组
斯特拉尔,地图!{| str | str.split(“”)}
#计算上、下、左、右公式
#向上=[-1][相同]
#向下=[+2][相同]
#左=[相同][1]
#右=[相同][+1]
#创建数据结构以存储路径
路径=[]
#创建通过每个数组中的每个元素循环的循环
内斯特拉尔
#然后对每个元素应用递归函数
最长路径=0
行=0
而行最长路径,则最长路径=result.length
col+=1
结束
行+=1
结束
#返回连接数
结果1.length-1
结束
#最长的XPath([“345”、“326”、“221”])
#正确答案:3,我的答案:3
#LongestMatrixPath([“12256”、“56219”、“43215”])
#正确答案:4,我的答案:3
#最长的XPath([“67”、“21”、“45”])
#正确答案:3,我的答案:1
#LongestMatrixPath([“111”、“111”、“111”])
#正确答案:0,我的答案:0
#LongestMatrixPath([“123”、“456”、“789”])
#正确答案:4,我的答案:4

此片段中存在问题:

    if up && up.to_i > current.to_i 
      recursiveFunction(array, (row-1), col, path)
    elsif down && down.to_i > current.to_i 
      recursiveFunction(array, (row+1), col, path)
    elsif left && left.to_i > current.to_i 
      recursiveFunction(array, row, (col-1), path)
    elsif right && right.to_i > current.to_i 
      recursiveFunction(array, row, (col+1), path)
    end
这里,如果
up
大于
current
,例如,
left
也大于
current
,则只会向上(因为
elsif
)。相反,你需要朝所有可能的方向走,选择最“有前途”的一个。为了实现这一点,我对你的代码做了一些修改,尽量少做修改。这是可行的,但可能不是最好的选择

    def recursiveFunction(array, row, col, original_path)
        current = array[row][col]
        row-1 >= 0  ? up = array[row-1][col] : up = nil 
        row+1 < array.length ? down = array[row+1][col] : down = nil 
        col-1 >= 0 ? left = array[row][col-1] : left = nil 
        col+1 < array[row].length ? right = array[row][col+1] : right = 
        nil 

        path = original_path.dup
        if !path.include?(current) && current.to_i > path[-1].to_i 
        path.push(current)
        # p path
        options = []
        if up && up.to_i > current.to_i 
            options << recursiveFunction(array, (row-1), col, path)
        end
        if down && down.to_i > current.to_i 
            options << recursiveFunction(array, (row+1), col, path)
        end
        if left && left.to_i > current.to_i 
            options << recursiveFunction(array, row, (col-1), path)
        end
        if right && right.to_i > current.to_i 
            options << recursiveFunction(array, row, (col+1), path)
        end

        best_path = options.sort_by{ |p| p.length }.last
        path += best_path.drop(path.length) if best_path
        end

        path 
    end
def recursiveFunction(数组、行、列、原始路径)
当前=数组[行][列]
第1行>=0?up=数组[第1行][col]:up=nil
行+1=0?左=数组[行][col-1]:左=零
列+1<数组[行]。长度?右=数组[行][col+1]:右=
无
路径=原始路径.dup
如果!路径。包括?(当前)和¤t.to_i>路径[-1]。to_i
路径推送(当前)
#p路径
选项=[]
如果向上(&up.to_i>current.to_i
选项当前至_i
选项当前至_i
选项当前至_i

选项您发布的代码部分太长。找出关键部分。关键部分是顶部的递归函数