JAVA Hole in Grid

巧了我就是萌 提交于 2019-12-24 16:33:40

问题


I need to find "holes" in a 2D grid in java - can you point me toward the best sort of algorithm to do this?

With the input of the following points:

5,3
5,4
8,4
5,5
6,3
7,3
7,4
6,5

I need to figure out the positions of the "hole" or surrounded space in this grid. I'm a bit lost as to how to do this.

Plot of the points:

Assuming each point is 1x1


回答1:


This is basically a blob extraction algorithm + a bit extra. Do this:

1) Find the westmost, eastmost, northmost and southmost any solid is placed. Remember them as xmin xmax ymin ymax.

2) Allocate an 2d array of integers (initialized to 0) with those dimensions, and place all solid points in it as the value -1.

3) Make a counter initialized to 1. Scan the 2d array. Every time you find a point that is 0, set it to counter and floodfill counters onto every adjacent point that is not a -1 until you've run out of points to floodfill onto. (To do a floodfill, one way is to keep a set of all points you haven't floodfilled all the neighbours of yet, and iterate over these, adding new points to the set until the set is exhausted -> nothing left to floodfill onto.) Now increment the counter and continue.

4) When you've scanned the whole grid, scan the perimeter. Every time you see a non -1 on the perimeter, mark that blob as not being surrounded (by having an array of bools as long as the number of blobs you found).

5) Every numbered blob you have not marked is surrounded.

Read about blob extraction here: http://en.wikipedia.org/wiki/Blob_extraction



来源:https://stackoverflow.com/questions/14793273/java-hole-in-grid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!