You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ruby多维数组与自定义函数:多数组邻接单元格处理咨询

Ruby多维数组单元格与邻接单元格处理实现

Let's walk through how to implement your grid-based cell processing logic in Ruby, including custom functions to handle each cell and its 8 adjacent neighbors.

1. First, Set Up Your Data Structure

Your example translates directly to Ruby syntax—here's how to define your multidimensional array properly:

t0 = [2, 1, 3, 2, 2, 2, 2, 1, 2, 3]
t1 = [1, 2, 1, 3, 2, 2, 2, 2, 1, 2]
t2 = [2, 1, 3, 2, 2, 2, 2, 1, 2, 3]
t3 = [2, 1, 1, 3, 1, 4, 2, 2, 1, 2]
t4 = [2, 1, 3, 1, 2, 2, 2, 2, 1, 2]
t = [t0, t1, t2, t3, t4]
instance1 = [t]

Just to clarify: instance1 is a 3D array where instance1[0] holds your 5 rows (t0-t4), instance1[0][i] is a single row array, and instance1[0][i][h] gives the value at column h of row i.

2. Define Neighbor Direction Offsets

To access the 8 adjacent cells, we'll use a list of coordinate offsets—this is a standard pattern for grid problems. Each offset pair represents the row and column shift from the current cell:

neighbor_offsets = [
  [-1, -1], [-1, 0], [-1, 1],  # Top-left, Top, Top-right
  [0, -1],          [0, 1],     # Left, Right
  [1, -1],  [1, 0], [1, 1]      # Bottom-left, Bottom, Bottom-right
]

3. Custom Function to Process Cells & Neighbors

Let's write a reusable function that handles checking the current cell's condition, validating neighbor bounds, and applying your update logic. We'll leave placeholders for you to replace with your specific rules:

def process_cell_and_neighbors(instance, row_idx, col_idx)
  # Get the current cell's value
  current_value = instance[0][row_idx][col_idx]
  
  # Replace this with your actual condition (e.g., check if value equals X)
  return unless current_value == 2
  
  grid_rows = instance[0].length
  grid_cols = instance[0][row_idx].length
  
  neighbor_offsets = [
    [-1, -1], [-1, 0], [-1, 1],
    [0, -1],          [0, 1],
    [1, -1],  [1, 0], [1, 1]
  ]
  
  neighbor_offsets.each do |row_offset, col_offset|
    # Calculate neighbor coordinates
    neighbor_row = row_idx + row_offset
    neighbor_col = col_idx + col_offset
    
    # Skip neighbors that are outside the grid bounds (avoids IndexError)
    next unless neighbor_row.between?(0, grid_rows - 1) && neighbor_col.between?(0, grid_cols - 1)
    
    # Replace this with your actual update logic (e.g., increment, set to a value)
    instance[0][neighbor_row][neighbor_col] += 1
    # Optional: Print updates for debugging
    puts "Updated cell (#{neighbor_row}, #{neighbor_col}) to #{instance[0][neighbor_row][neighbor_col]}"
  end
end

4. Iterate Over Your Target Cells

Your example uses ranges 1..3 (columns) and 1..8 (rows). Since Ruby arrays are 0-indexed, this means we're processing rows 1 to 8 (inclusive) and columns 1 to 3 (inclusive) of your grid. Here's how to run the function over those cells:

# Process the specific range from your example
(1..8).each do |row_idx|
  (1..3).each do |col_idx|
    process_cell_and_neighbors(instance1, row_idx, col_idx)
  end
end

If you ever need to process every cell in the grid instead of a subset, use this loop instead:

instance1[0].each_with_index do |row, row_idx|
  row.each_with_index do |_value, col_idx|
    process_cell_and_neighbors(instance1, row_idx, col_idx)
  end
end

5. Full Working Example

Putting it all together, here's the complete code you can run and adapt:

# Define your grid data
t0 = [2, 1, 3, 2, 2, 2, 2, 1, 2, 3]
t1 = [1, 2, 1, 3, 2, 2, 2, 2, 1, 2]
t2 = [2, 1, 3, 2, 2, 2, 2, 1, 2, 3]
t3 = [2, 1, 1, 3, 1, 4, 2, 2, 1, 2]
t4 = [2, 1, 3, 1, 2, 2, 2, 2, 1, 2]
t = [t0, t1, t2, t3, t4]
instance1 = [t]

# Custom processing function
def process_cell_and_neighbors(instance, row_idx, col_idx)
  current_value = instance[0][row_idx][col_idx]
  
  # Custom condition: adjust to match your needs
  return unless current_value == 2
  
  neighbor_offsets = [
    [-1, -1], [-1, 0], [-1, 1],
    [0, -1],          [0, 1],
    [1, -1],  [1, 0], [1, 1]
  ]
  
  grid_rows = instance[0].length
  grid_cols = instance[0][row_idx].length
  
  neighbor_offsets.each do |row_offset, col_offset|
    neighbor_row = row_idx + row_offset
    neighbor_col = col_idx + col_offset
    
    next unless neighbor_row.between?(0, grid_rows - 1) && neighbor_col.between?(0, grid_cols - 1)
    
    # Custom update logic: replace with your desired operation
    instance[0][neighbor_row][neighbor_col] += 1
  end
end

# Process the specified cell range
(1..8).each do |row_idx|
  (1..3).each do |col_idx|
    process_cell_and_neighbors(instance1, row_idx, col_idx)
  end
end

# Print final grid state to verify changes
puts "Final Grid State:"
instance1[0].each do |row|
  puts row.inspect
end

Key Notes

  • Bounds Checking: Always validate neighbor coordinates to avoid IndexError when cells are on the edge of the grid.
  • Customization: Adjust the condition (current_value == 2) and update logic (instance[0][neighbor_row][neighbor_col] +=1) to match your exact requirements.
  • 0-Indexing: Ruby uses 0-indexed arrays, so make sure your ranges align with the actual positions you want to target.

内容的提问来源于stack exchange,提问作者Nigele2

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:37:16