跳至主要內容

岛屿的数量


岛屿的数量

题目

给定一个由岛屿(1表示)和水(0表示)组成的二维网格,请计算网格中岛屿的数量。岛屿总是被水包围,并且每座岛屿只能由水平方向或垂直方向上相邻的陆地连接形成。此外,可以假设该网格的四条边均被水包围。

示例1:

输入:grid=[
    ["1","1","1","1","0",],
    ["1","1","0","1","0",],
    ["1","1","0","0","0",],
    ["0","0","0","0","0",],
]
输出:1

示例2:

输入:grid=[
    ["1","1","0","0","0",],
    ["1","1","0","0","0",],
    ["0","0","1","0","0",],
    ["0","0","0","1","1",],
]
输出:3

解答

class Solution {
    private int res;
    public int numIslands(char[][] grid) {
        res = 0;
        //遍历矩阵网格,方便开始其他岛屿的遍历,dfsGrid只具备遍历一格岛屿的能力,不能从一个岛屿跳到另一个岛屿去遍历
        //遍历矩阵就是为了从一个岛屿跳到另一个岛屿去遍历
        for (int i = 0; i < grid.length; i ++) {
            for (int j = 0; j < grid[0].length; j ++) {
                if (grid[i][j] == '1') {
                    dfsGrid(grid, i, j);
                    res ++;
                }
            }
        }
        return res;
    }

    //dfsGrid的作用是,在上面的循环中调用dfsGrid(给定一个初始坐标),然后dfsGrid从这个初始坐标开始遍历,
    //标记与这个初始坐标直接或间接相邻的陆地,这些陆地组成一个岛屿
    private void dfsGrid(char[][] grid, int row, int col) {
        //判断坐标是否(row,col)是否在网格中,如果不在则返回
        if (row >= grid.length || col >= grid[0].length || row < 0 || col < 0) {
            return;
        }

        if (grid[row][col] != '1') {
            return;
        }

        grid[row][col] = '2';//将格子标记为已经遍历过
    
        dfsGrid(grid, row - 1, col);
        dfsGrid(grid, row + 1, col);
        dfsGrid(grid, row, col - 1);
        dfsGrid(grid, row, col + 1);
    }
}
上次编辑于:
贡献者: Neil