Sobes.tech
Junior — Senior

Counting the number of islands in a 2D grid

livecode

Task condition

Given a map represented as a two-dimensional array char[][], where the character '1' denotes land and '0' denotes water. It is necessary to calculate the number of islands. An island is a connected group of land cells, connected horizontally or vertically (diagonal connections are not considered).

public static int countIslands(char[][] land) {
    // TODO
}
char[][] islandMap1 = {
    {'1', '1', '0'},
    {'0', '1', '0'},
    {'0', '0', '1'}
};

char[][] islandMap2 = {
    {'1', '0', '0', '0'},
    {'0', '1', '0', '0'},
    {'0', '0', '1', '0'},
    {'0', '0', '0', '1'}
};