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

如何在Ruby多维数组中检测对角线连续4个相同元素

Detecting 4 Consecutive Identical Elements in Diagonals

Got it, let's break down how to detect that diagonal sequence of 4 matching elements in your 5x5 array. We'll cover both possible diagonal directions (top-left to bottom-right, and top-right to bottom-left) using loops and conditional checks to verify the sequence.

Key Observations

  • For a 5x5 array, a valid 4-element diagonal can only start at specific positions:
    • Top-left to bottom-right: Starting rows/columns must leave room for 3 more elements (so rows 0-1, columns 0-1).
    • Top-right to bottom-left: Starting rows 0-1, columns 3-4 (since we need to move left 3 times without going out of bounds).
  • We’ll skip checking sequences of "0" (optional, but useful to avoid triggering on the initial all-zero state).

Example Implementation (Python)

Here’s a concrete code snippet that implements the check:

double_array = [ ["1", "0", "0", "0", "0"], 
                 ["0", "1", "0", "0", "0"], 
                 ["0", "0", "1", "0", "0"], 
                 ["0", "0", "0", "1", "0"], 
                 ["0", "0", "0", "0", "0"], ]

# Flag to track if we found a valid diagonal sequence
found_diagonal = False

# Check top-left to bottom-right diagonals
for i in range(2):  # Rows 0 and 1
    for j in range(2):  # Columns 0 and 1
        current_val = double_array[i][j]
        # Skip if current element is 0 (remove this line if you want to detect 0 sequences)
        if current_val == "0":
            continue
        # Verify the next 3 elements in the diagonal match
        if (current_val == double_array[i+1][j+1] and
            current_val == double_array[i+2][j+2] and
            current_val == double_array[i+3][j+3]):
            found_diagonal = True
            break
    if found_diagonal:
        break

# If not found yet, check top-right to bottom-left diagonals
if not found_diagonal:
    for i in range(2):  # Rows 0 and1
        for j in range(3, 5):  # Columns3 and4
            current_val = double_array[i][j]
            if current_val == "0":
                continue
            if (current_val == double_array[i+1][j-1] and
                current_val == double_array[i+2][j-2] and
                current_val == double_array[i+3][j-3]):
                found_diagonal = True
                break
        if found_diagonal:
            break

# Execute your desired operation if a diagonal is detected
if found_diagonal:
    print("Detected 4 consecutive identical elements in a diagonal!")
    # Add your custom operation here (e.g., update the array, trigger a game event)
else:
    print("No valid diagonal sequence found.")

How It Works

  1. Loop Through Valid Starts: We iterate over all possible starting positions for both diagonal directions.
  2. Skip Zeros: Optional check to ignore the initial all-zero state (remove this if you need to detect sequences of zeros).
  3. Conditional Check: The core if statement verifies that the current element matches the next three elements in the diagonal.
  4. Early Termination: Once a valid sequence is found, we set the flag and break out of loops early to save computation.
  5. Execute Operation: Finally, we check the flag and run your desired logic if the sequence was detected.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:08:10