How can I find hole in a 2D matrix?

后端 未结 5 2156
死守一世寂寞
死守一世寂寞 2020-12-14 09:30

I know the title seems kind of ambiguous and for this reason I\'ve attached an image which will be helpful to understand the problem clearly. I need to find holes inside the

5条回答
  •  温柔的废话
    2020-12-14 09:53

    You can represent the grid as a graph with individual cells as vertexes and edges occurring between adjacent vertexes. Then you can use Breadth First Search or Depth First Search to start at each of the cells, on the sides. As you will only find the components connected to the sides, the black cells which have not been visited are the holes. You can use the search algorithm again to divide the holes into distinct components.

    EDIT: Worst case complexity must be linear to the number of cells, otherwise, give some input to the algorithm, check which cells (as you're sublinear, there will be big unvisited spots) the algorithm hasn't looked into and put a hole in there. Now you've got an input for which the algorithm doesn't find one of the holes.

提交回复
热议问题