433. Number of Islands-岛屿的个数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lighthear/article/details/79840033

Description

给一个01矩阵,求不同的岛屿的个数。

0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛。我们只考虑上下左右为相邻。

样例

在矩阵:

[
  [1, 1, 0, 0, 0],
  [0, 1, 0, 0, 1],
  [0, 0, 0, 1, 1],
  [0, 0, 0, 0, 0],
  [0, 0, 0, 0, 1]
]

中有 3 个岛.

Solution

算法思路:

1. 遍历矩阵中所有的点,如果是1,那就先把这个点改为0。

2. 然后从这个点进入,开始BFS上搜索,找到所有以1连通的点,并将他们改为0,知道周围没有连通的1。

3. 循环这个过程,每次在矩阵中找到1的入口都让计数值加一。

class Coordinate {
    int x, y;
    public Coordinate(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
public class Solution {
    /**
     * @param grid: a boolean 2D matrix
     * @return: an integer
     */
    public int numIslands(boolean[][] grid) {
        // write your code here
        //遍历矩阵中的每一个点
        //如果是1,那就先改为0,然后从这个点进入
        //用BFS找到周围(上下左右)的所有为1的连通的点,并改成0
        if (grid == null || grid.length == 0 || grid[0].length == 0) {
            return 0;
        }
        
        int n = grid.length;
        int m = grid[0].length;
        int islands = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (grid[i][j]) {
                    markByBFS(grid, i, j);
                    islands++;
                }
            }
        }
        return islands;
    }
    private void markByBFS(boolean[][] grid, int x, int y) {
        //建立坐标变换数组,然后for循环,减少4次if判断的冗余代码
        int[] directionX = {0, 1, 0, -1};
        int[] directionY = {1, 0, -1, 0};
        Queue<Coordinate> queue = new LinkedList<>();
        queue.offer(new Coordinate(x, y));
        grid[x][y] = false;
        while (!queue.isEmpty()) {
            Coordinate coor = queue.poll();
            for (int i = 0; i < 4; i++) {
                Coordinate adj = new Coordinate(
                    coor.x + directionX[i],
                    coor.y + directionY[i]
                );
                if (!inBound(adj, grid)) {
                    continue;
                }
                if (grid[adj.x][adj.y]) {
                    grid[adj.x][adj.y] = false;
                    queue.offer(adj);
                }
            }
        }
    }
    //判断点是否在矩阵范围内
    private boolean inBound(Coordinate coor, boolean[][] grid) {
        int n = grid.length;
        int m = grid[0].length;
        return coor.x >= 0 && coor.x < n && coor.y >= 0 && coor.y < m;
    }
}

猜你喜欢

转载自blog.csdn.net/lighthear/article/details/79840033