检测2D阵列中的组合

时间:2018-03-02 20:34:28

标签: java arrays multidimensional-array

有人能指出我正确的方向吗? 我试图提出一种算法,检查行,列或对角线中是否有3个相同的值。应该为此测试数组的所有元素。此外,2d数组大小可以是1到10.我已经尝试了各种实现,但他们没有正确测试它。

例如,在第一个元素被测试时找到XXX但是如果测试了中间元素则它没有标记

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

<强>算法

Loop through every row in the 2d List:
  Loop through every element of the row:

    Check if the element is the first one of its row:
      If yes, check if the next two are the same as this one. If yes, you've found a match.
    Else check if the element is the last one of its row:
      If yes, check if the previous two are the same as this one. If yes, you've found a match.
    Else:
      Check if the previous and next ones are the same as this one. If yes, you've found a match.

    If it's not a match:
      Check if the element is in the first row:
          If yes, check if the two below are the same as this one. If yes, you've found a match.
      Else check if the element is in the last row:
          If yes, check if the two above are the same as this one. If yes, you've found a match.
      Else:
          Check if the above and below ones are the same as this one. If yes, you've found a match.