Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.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
用Ruby实现二维数组_Ruby_Arrays - Fatal编程技术网

用Ruby实现二维数组

用Ruby实现二维数组,ruby,arrays,Ruby,Arrays,我试图在一个二维数组中,搜索任何大于0的空间,然后将周围的空间增加1 array = [[0,0,0], [0,1,0], [0,0,0]] 最终输出应该如下所示 new_array = [[0,1,0], [1,1,1], [0,1,0]] 我目前的设置方式正在恢复 [1,1,1,1] 它增加了正确的空格,我想,我只是不知道如何将它们放回原始数组,然后返回2D数组。很明显,缺少一些步骤,任何帮助都将不胜感激。我理解为什么它会恢复原状,只是不清楚下一步该怎么做 def dilate(arr

我试图在一个二维数组中,搜索任何大于0的空间,然后将周围的空间增加1

array = [[0,0,0], [0,1,0], [0,0,0]]
最终输出应该如下所示

new_array = [[0,1,0], [1,1,1], [0,1,0]]
我目前的设置方式正在恢复

[1,1,1,1]
它增加了正确的空格,我想,我只是不知道如何将它们放回原始数组,然后返回2D数组。很明显,缺少一些步骤,任何帮助都将不胜感激。我理解为什么它会恢复原状,只是不清楚下一步该怎么做

def dilate(array)
  new_array = []
  array.each_with_index do |row, rowIndex|
    row.each_with_index do |col, colIndex|
      if (array[rowIndex][colIndex] > 0)
        new_array << (array[rowIndex][colIndex -1] +1)
        new_array << (array[rowIndex -1][colIndex] +1)
        new_array << (array[rowIndex][colIndex +1] +1)
        new_array << (array[rowIndex +1][colIndex] +1)
      end
    end
  end
  return new_array
end
def扩展(阵列)
新的_数组=[]
array.each_with_index do |行,行索引|
行。每个带有索引do的列,共索引|
if(数组[rowIndex][colIndex]>0)

新建数组您要做的是初始化一个空数组,
新建数组
,并向其添加元素。将递增的值追加到新数组中,而不是更改数组中的值。像这样的方法应该会奏效:

def dilate(array)
  new_array=[]
  array.each{|i|new_array<<i.dup}
  array.each_with_index do |row, rowIndex|
    row.each_with_index do |col, colIndex|
      if (array[rowIndex][colIndex] > 0)
        new_array[rowIndex][colIndex -1] += 1 if colIndex > 0
        new_array[rowIndex -1][colIndex] += 1 if rowIndex > 0
        new_array[rowIndex][colIndex +1] += 1 if colIndex < array.first.size-1
        new_array[rowIndex +1][colIndex] += 1 if rowIndex < array.size-1
      end
    end
  end
  return new_array
end
def扩展(阵列)
新的_数组=[]
数组。每个{i}新的{u数组0
如果rowIndex>0,则新建_数组[rowIndex-1][colIndex]+=1
如果colIndex
我正在创建一个新数组,
new\u array
,它是
array
的副本,然后递增其元素


顺便说一句,你在问题中说你想“搜索任何包含1的空间”,但这是在搜索包含大于零的值的空间。我不确定这是否是你想要的。

我建议你为此使用该类。它使操作简单,易于阅读

代码

require 'matrix'

def dilate(arr)
  nrows, ncols = arr.size, arr[0].size
  m = Matrix[*arr]
  m.each_with_index.reduce(m.dup) { |mout, (e, row, col)|
    mout + Matrix.build(nrows, ncols) { |i,j|
      adjacent?(i,j,row,col) ? e : 0 } }.to_a
end

def adjacent?(r0,c0,r1,c1)
  ((r0-r1).abs == 1 && (c0==c1)) || ((c0-c1).abs == 1 && (r0==r1))
end
我假设,当元素
x
的值非零时,您希望将相邻元素的值增加
x
。如果您希望将相邻元素的值增加
1
,请更改:

Matrix.build(nrows, ncols) { |i,j| adjacent?(i,j,row,col) ? e : 0 }
致:

示例

arr = [[0, 0, 0],
       [0, 1, 0],
       [0, 0, 0]]           
dilate(arr)
  #=> [[0, 1, 0],
  #    [1, 1, 1],
  #    [0, 1, 0]] 

arr = [[0, 0, 0, 0],
       [0, 1, 0, 0],
       [0, 0, 0, 0]]           
dilate(arr)
  #=> [[0, 1, 0, 0],
  #    [1, 1, 1, 0],
  #    [0, 1, 0, 0]]

arr = [[1, 0, 0],
       [0, 0, 0],
       [0, 0, 0]]
dilate(arr)
  #=> [[1, 1, 0],
  #    [1, 0, 0],
  #    [0, 0, 0]] 


首先,谢谢你这么快回复你的答案。是的,我想说的是大于零。我在irb中测试了它,但我得到了未定义的方法+用于nil类。不确定这是否是我正在做的事情?看起来它应该能工作……有什么想法吗?@DannySullivan是的,第二行是错的。我复制了原始数据最后一个数组。它现在已经修好了。顺便说一句,它没有就位。这是你想要的吗?请仔细检查你的工作。我没有得到正确的结果。1:
arr=[[1,0],[0,0]];deplate(arr)=>[1,2],[2,0]
[2:
arr=[[1,0,0],[0,0];deplate(arr)=[1,1,0],[0,0]]
#3
命名错误:nil:NilClass的未定义方法“+”
用于我答案中的最后一个(4x4)示例。问题是,如果
colIndex==0
新数组[rowIndex][colIndex-1]+=1
变为
新数组[rowIndex][1]+=1
,这将在数组的最后一个元素
新数组[rowIndex]中添加一个元素
。如果colIndex>0,您需要的是
new\u数组[rowIndex][colIndex-1]+=1
。类似于
new\u数组[rowIndex-1][colIndex]+=1
。如果colIndex
new\u数组[rowIndex+1][colIndex],您还需要
new u数组[rowIndex][colIndex+1]+=1]如果rowIndex
,则+=1。提醒大家注意Ruby约定,变量和方法的名称应为snake\u case;例如
row\u index
col\u index
。感谢@CarySwoveland的帮助。我将检查我的工作,并测试一下您的建议。
arr = [[0, 0, 0],
       [0, 1, 0],
       [0, 0, 0]]           
dilate(arr)
  #=> [[0, 1, 0],
  #    [1, 1, 1],
  #    [0, 1, 0]] 

arr = [[0, 0, 0, 0],
       [0, 1, 0, 0],
       [0, 0, 0, 0]]           
dilate(arr)
  #=> [[0, 1, 0, 0],
  #    [1, 1, 1, 0],
  #    [0, 1, 0, 0]]

arr = [[1, 0, 0],
       [0, 0, 0],
       [0, 0, 0]]
dilate(arr)
  #=> [[1, 1, 0],
  #    [1, 0, 0],
  #    [0, 0, 0]] 
arr = [[0, 0, 0],
       [1, 0, 0],
       [0, 0, 0]]
dilate(arr)
  #=> [[1, 0, 0],
  #    [1, 1, 0],
  #    [1, 0, 0]] 

arr = [[0, 0, 1],
       [0, 0, 0],
       [0, 0, 0]]
dilate(arr)
  #=> [[0, 1, 1],
  #    [0, 0, 1],
  #    [0, 0, 0]]

arr = [[0, 0, 0, 3],
       [0, 1, 1, 0],
       [0, 4, 1, 0],
       [2, 0, 0, 0]]    
dilate(arr)
  #=> [[0, 1, 4, 3],
  #    [1, 6, 3, 4],
  #    [6, 6, 6, 1],
  #    [2, 6, 1, 0]]