can counting contiguous regions in a bitmap be improved over O(r * c)?

前端 未结 5 1088
南笙
南笙 2021-02-15 16:52

You are given an image of a surface photographed by a satellite.The image is a bitmap where water is marked by \'.\' and land is marked by \'*\'. Adjacent group of

5条回答
  •  执笔经年
    2021-02-15 17:25

    1. Create a undirected graph, where each island node connects to its neighboor island nodes.

    2. While there are unvisited nodes:

      • pick an unvisited node, do a depth-first traversal and mark every node visited, increase number_of_islands.
    3. Done.

    Both (1) and (2) takes O(n) time.

提交回复
热议问题