如何计算二维数组中相同单元格的组? [英] How to count groups of same cells in a 2d array?
本文介绍了如何计算二维数组中相同单元格的组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是示例(算出黑色的):
Here's the example (counting black ones):
输入:
输出:
5 4 // 5 groups (4 squares each)
1 1 // 1 group containing 1 square
现在,我想不出什么比痛苦的迭代更好的了。是否可以递归方式获取这些组?
谢谢
For Now, I can't think of anything better than a painfull for iteration. Would it be possible to get these groups in a recursive way? Thanks
推荐答案
将所有黑色正方形设置为节点。黑色正方形之间的连接(如果正方形彼此相邻)将是一个边缘。
Set all black squares as nodes. Connection between black squares (if the squares are next to each other) will be an edge.
这为您提供了图。
A DFS 将为您提供所有组。请注意,DFS本质上是递归的。
A DFS in the graph will get you all the groups. Note that DFS is recursive by nature.
这篇关于如何计算二维数组中相同单元格的组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文