Calculating un-floodfill-able area

时间:2018-02-26 17:42:37

标签: java matrix graphics field flood-fill

I have a 2D array (n * m) as a canvas and coordinates of Used points (startingPoints) in it. I have to calculate, how many points in it is not fieldable with floodfill from outside of the canvas. Field can go only in 4 directions and there are three types of points: Free, Fielded, Used. Used point is not fieldable and field not go over it. So count of unfieldable points is n*m-fieldable-startingPoints.

Now I do it like this: I run floodfill with stack from every point of border and I calculate, how many points are fielded.

But this is not usable for canvas, with dimensions 10^18*10^18. This wants a lot of memory and I must find better solution than using this classic floodfill.

Can someone help with better solution?

1 个答案:

答案 0 :(得分:0)

我不是100%确定我理解你想要的东西。

我想象的是一个迷宫,在外边缘有多个可能的起点,你想知道从外面可以访问/填充的所有可能区域吗?

提高效率的解决方案是缓存值,这样您就不会重新计算已经计算过的填充。

制作另一个大小相等的2d数组,以跟踪已填充的点。

喜欢的东西 byte[][] filledPoints = new byte[n][m];初始化为0,表示未填充。

仅举例:

final byte UNFILLED = 0;
final byte RED = 1;
final byte BLUE = 2;

当您进行填充时,对于您访问的每个点,使用“填充颜色”标记该点以表示它已经计算过。

EG。 filledPoints[0][1] = RED

但是,在进行填充时,如果您正在填充的点已经填充,那么您将要执行的整个填充已经由之前的填充计算。

if(filledPoints[x][y] != UNFILLED){
    // Then this fill was already filled with the color of filledPoint[x][y]
}

因此,如果它已经计算过,那么您不需要再次计算它,所以继续前进到您要尝试填充的下一个起点,并检查它是否已经填充。

如果找到一个尚未填充的填充区域,则开始用新的“颜色”填充它,并跟踪填充的区域。

EG。 filledPoints[0][386] = BLUE

非洪水可填充区域当然是总面积减去所有洪水填充区域的总和。