检测2D阵列中的组合

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了检测2D阵列中的组合相关的知识,希望对你有一定的参考价值。

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

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

任何帮助表示赞赏。

答案

算法:

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.

以上是关于检测2D阵列中的组合的主要内容,如果未能解决你的问题,请参考以下文章

2d 阵列 gamemaker2 工作室

Python将文本文件读入2D阵列并访问数据

基于像素的标签将2D像素阵列更改为图像

对于具有数百万像素的 2D 未装箱像素阵列,建议使用哪种 Haskell 表示?

将2D阵列分成由连续线限定的区域

Unity 2d 中的碰撞检测