Floodfill (Recursive)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
int visited[MaxX][MaxY];
int currSize = 0;

int xv[] = {0, 0, -1, 1};
int yv[] = {-1, 1, 0, 0};
void ff(int r, int c, int color) {
    // >= N or >? Does M exist?
    if (r >= N || r < 0 || c >= M || c < 0 || visited[r][c]) {
        return;
    }

    currSize++;
    visited[r][c] = color;

    F0R(i, 4) {
        int nX = r + xv[i], nY = c + yv[i];
        ff(nX, nY, color);
    }
}

int floodfill() {

    memset(visited, 0, MaxX * MaxY * sizeof(bool));

    int color = 0;
    F0R(i, N) {
      F0R(j, M) {
        if (! visited[i][j]) {
          currSize = 0;
          color++;
          ff(i, j, color);
        }
      }
    }

    return color;
}