在多维数组中查找最长的连接元素

时间:2013-02-14 12:16:33

标签: java arrays algorithm

如何在大小为4 * 5的数组中找到最长的连接元素。如果值在水平或垂直方向相同,则称两个元素连接。如果对角线存在,则值不相同。

数组是

        [ B C D A D

          D C D A B

          D C C C A

          C D B A B ]

这里最长的序列是5,它的C

这可以使用Longest Common Subsequence algorthim进行整理,但不知道如何准确地实现它。

有谁可以帮我解决这个问题?

1 个答案:

答案 0 :(得分:0)

您可以使用flood fill algorithm检查已连接元素的组。

这就是我要实施的算法:

for each field of the array
    when the field is not marked as checked
        use flood-fill to get the connected group the field belongs to
        for each field which is part of the group
            mark it as checked        
        when this group got more fields than the larges known group
            this group is now the largest known group