I want Flood Fill without stack and without recursion

I wanted to know how to apply the fill fill to an array, my array is two-dimensional, which contains temporary characters of a Roman font of type letter. The border line contains 1 inside and outside of all 0. I want to fill all 1 instead of 0 only inside. But I need logic that does not require more memory. Therefore avoid recursion and stack or queue

+3
source share
4 answers

I usually don’t do homework for other people, but I liked the problem:

int c = -1;
while (c < 0)
{    
    /* Store breadcrumb trail, look to carry on */
    a[x][y] = c--;
    if (!hunt(0))
    {
        /* Nowhere to go, so back-track by looking for breadcrumb */
        a[x][y] = 1;
        c += 2;
        hunt(c);
    }
}

bool_t hunt(int v)
{
    if (a[x-1][y] == v)  { x--; return TRUE; }
    if (a[x+1][y] == v)  { x++; return TRUE; }
    if (a[x][y-1] == v)  { y--; return TRUE; }
    if (a[x][y+1] == v)  { y++; return TRUE; }
    return FALSE;
}

, . , , , , int s, 0 1 .

+4

. , , . , , , .

( ), ( ):

( , ), , , ().

, ( O):

00010010001001000
   ^  ^   ^  ^
   |  |   |  stop
   |  |   start
   |  stop
   start

:

00011110001111000

, .

+1
function LowMemFloodFill(pixel)
  FillPixel(pixel)
  Do
    didFill = false
    For each pixel
      If current pixel has been filled
        For each adjacent pixel
          If adjacent has not been filled
            FillPixel(adjacent)
            didFill = true
          End
        End
      End
    End
  While didFill
End

, , ( ). , .

0

You basically cannot. You should keep this information somewhere because you need to know where else to start filling out after you finish your current section. Recursion allows you to do this implicitly. Saving your own stack allows you to do this explicitly, possibly with saving. Oli Charlesworth does a nice thing by storing an array the same size as the image, but it uses even more memory than recursing or storing a position stack.

-1
source

Source: https://habr.com/ru/post/1771047/


All Articles