如何从递归函数返回数组

时间:2015-08-08 22:54:59

标签: ruby

我有一个板(多维数组)。您从左上角开始,可以向右或向下移动。基本案例是右下角。

我制作了一个功能,可以在棋盘中查找所有可能的移动及其值。到达基本案例时,移动和值将存储为数组ARR_SUMNUM

ARR_SUMNUM = []
def arr_all_moves(ary,a=0,b=0,sum_num=0,directions="")
  rows = ary.length
  cols = ary[0].length
  goal = rows-1 + cols-1
  curr_num = ary[a][b]
  sum_num += curr_num
  if [a,b] == [rows-1,cols-1]
    ARR_SUMNUM.push([sum_num, directions])
    return
  end
  if a == rows -1 
    return arr_all_moves(ary,a,b+1,sum_num,directions+="right ")
  elsif b == cols -1
    return arr_all_moves(ary,a+1,b,sum_num,directions+="down ")
  end
  arr_all_moves(ary,a,b+1,sum_num,directions+="right ")
  directions.chomp!("right ") ##### I realize this was the source of all my issues
  arr_all_moves(ary,a+1,b,sum_num,directions+="down ")
  return ARR_SUMNUM
end

board = [   [1,3,4],
            [5,6,999],
            [8,9,10],
            [11,12,13],
            [2000,42,13]
        ]
p arr_all_moves(board)

如何在没有ARR_SUMNUM之类的常量的情况下使用此函数并使用局部变量?我尝试使用arr_sumnum代替,但我得到一个空数组。

3 个答案:

答案 0 :(得分:0)

最简单的方法可能是通过将数组设置为方法参数之一来将数组传递给每行调用。

def arr_all_moves(ary, a=0, b=0, sum_num=0, directions=" ", arr_sum_num=[])
  rows = ary.length
  cols = ary[0].length
  goal = rows-1 + cols-1
  curr_num = ary[a][b]
  sum_num += curr_num
  if [a,b] == [rows-1,cols-1]
    arr_sum_num.push([sum_num, directions])
    return
  end
  if a == rows -1
    return arr_all_moves(ary,a,b+1,sum_num,directions+="right ", arr_sum_num)
  elsif b == cols -1
    return arr_all_moves(ary,a+1,b,sum_num,directions+="left ", arr_sum_num)
  end
  arr_all_moves(ary,a,b+1,sum_num,directions+="right ", arr_sum_num)
  arr_all_moves(ary,a+1,b,sum_num,directions+="left ", arr_sum_num)
  return arr_sum_num
end

答案 1 :(得分:0)

有两种方法,一种已经提到过涉及将数组传播到递归函数调用。另一个是实际向上传播数组并将它们添加到调用函数中。

def arr_all_moves(ary,a=0,b=0,sum_num=0,directions=" ")
  rows = ary.length
  cols = ary[0].length
  goal = rows-1 + cols-1
  curr_num = ary[a][b]
  sum_num += curr_num
  if [a,b] == [rows-1,cols-1]
    return [[sum_num, directions]]
  end
  if a == rows -1 
    return arr_all_moves(ary,a,b+1,sum_num,directions+="right ")
  elsif b == cols -1
    return arr_all_moves(ary,a+1,b,sum_num,directions+="left ")
  end
  return arr_all_moves(ary,a,b+1,sum_num,directions+="right ") + 
         arr_all_moves(ary,a+1,b,sum_num,directions+="left ")
end

board = [   [1,3,4],
            [5,6,999],
            [8,9,10],
            [11,12,13],
            [2000,42,13]
        ]
p arr_all_moves(board)

答案 2 :(得分:0)

以下是一种可能的解决方案,您应该使用down代替left,因为我们正在向右或向下穿过2-D阵列以到达右下角。

def arr_all_moves(ary, a=0, b=0, sum_num=0, directions=" ")
  rows = ary.length
  cols = ary[0].length

  sum_num += ary[a][b]

  if [a,b] == [rows-1,cols-1]
    [] << [sum_num, directions]
  elsif a == rows-1
    arr_all_moves(ary, a, b+1, sum_num, directions+="right ")
  elsif b == cols-1
    arr_all_moves(ary, a+1, b, sum_num, directions+="down ")
  else
    arr_all_moves(ary, a, b+1, sum_num, directions+="right ") + 
    arr_all_moves(ary, a+1, b, sum_num, directions+="down ")    
  end
end

board = [  
          [1,3,4],
          [5,6,999],
          [8,9,10],
          [11,12,13],
          [2000,42,13]
        ]

p arr_all_moves(board)