ProgrammingVery hardCompetitive coding (CP)

What is printed?

What is printed?
#include <stdio.h>
char g[3][4] = {"110", "010", "011"};
int fill(int r, int c) {
    if (r < 0 || c < 0 || r >= 3 || c >= 3 || g[r][c] != '1') return 0;
    g[r][c] = '0';
    return 1 + fill(r + 1, c) + fill(r - 1, c) + fill(r, c + 1) + fill(r, c - 1);
}
int main(void) {
    printf("%d", fill(0, 0));
}
  1. A. 4
  2. B. 3
  3. C. 6
  4. D. 5

Correct answer: D. 5

The flood fill from (0,0) reaches (0,1), (1,1), (2,1) and (2,2), so the component has five cells. The cell (1,0) is 0 and blocks nothing else.

More C Programming questions

Exams that ask this topic