ProgrammingHard

Count the 4-connected islands of 1s in this grid, where rows are "11000", "11010", "00100", "00011". How many islands are there?

  1. A. 2
  2. B. 3
  3. C. 5
  4. D. 4

Correct answer: D. 4

The islands are the top-left 2 x 2 block, the lone 1 in row 1, the lone 1 in row 2, and the pair at the bottom right. Diagonal neighbours do not connect, so the lone cells stay separate.

More C Programming questions

Exams that ask this topic