How to count groups of same cells in a 2d array?

后端 未结 3 812
清酒与你
清酒与你 2020-12-31 14:08

Here\'s the example (counting black ones):

input:

\"enter

output:

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-31 14:40

    Set all black squares as nodes. Connection between black squares (if the squares are next to each other) will be an edge.

    This gives you a graph.

    A DFS in the graph will get you all the groups. Note that DFS is recursive by nature.

提交回复
热议问题