检测2D阵列中的组合

问题描述 投票:0回答:1

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

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

任何帮助表示赞赏。

java arrays multidimensional-array
1个回答
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.
© www.soinside.com 2019 - 2024. All rights reserved.