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

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

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

input:

\"enter

output:

3条回答
  •  灰色年华
    2020-12-31 14:26

    At the beginning, each cell be "unvisited".

    I would iterate through the cells until you meet an "unvisited" black cell. Each white cell you hit up to that point

    Once you hit a black cell, you "expand" it to all directions if possible (similar to "floodfilling"). You expand as long as you can and mark all the visited cells as "visited". After you did that, you count how many black cells you infected, and you know how big the group was. After detecting the group, you go on to the next "unvisited" black cell.

提交回复
热议问题