241105-200

35 阅读2分钟

给你一个由 '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

思路

1.直观的想法是DFS递归染色,比较容易想到
2.连通性问题也可以想到并查集,二维如何运用呢?

UF

用i*w+j来摊平二维数组为1维,然后遇到岛屿节点就向右向下把相邻的岛屿都union一次,最终得到的联通两count-水节点数量=岛屿联通数量

class Solution {

    private int rows;
    private int cols;

    public int numIslands(char[][] grid) {
        rows = grid.length;
        if (rows == 0) {
            return 0;
        }
        cols = grid[0].length;

        // 空地的数量
        int spaces = 0;
        UnionFind unionFind = new UnionFind(rows * cols);
        int[][] directions = {{1, 0}, {0, 1}};
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (grid[i][j] == '0') {
                    spaces++;
                } else {
                    // 此时 grid[i][j] == '1'
                    for (int[] direction : directions) {
                        int newX = i + direction[0];
                        int newY = j + direction[1];
                        // 先判断坐标合法,再检查右边一格和下边一格是否是陆地
                        if (newX < rows && newY < cols && grid[newX][newY] == '1') {
                            unionFind.union(getIndex(i, j), getIndex(newX, newY));
                        }
                    }
                }
            }
        }
        return unionFind.getCount() - spaces;
    }

    private int getIndex(int i, int j) {
        return i * cols + j;
    }

    private class UnionFind {
        /**
         * 连通分量的个数
         */
        private int count;
        private int[] parent;

        public int getCount() {
            return count;
        }

        public UnionFind(int n) {
            this.count = n;
            parent = new int[n];
            for (int i = 0; i < n; i++) {
                parent[i] = i;
            }
        }

        private int find(int x) {
            while (x != parent[x]) {
                parent[x] = parent[parent[x]];
                x = parent[x];
            }
            return x;
        }

        public void union(int x, int y) {
            int xRoot = find(x);
            int yRoot = find(y);
            if (xRoot == yRoot) {
                return;
            }

            parent[xRoot] = yRoot;
            count--;
        }
    }
}

DFS

public int numIslands(char[][] grid) {
    int h = grid.length;
    int w = grid[0].length;
    int count = 0;
    //首先先到的是递归
    boolean[][] visited = new boolean[h][w];
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (grid[i][j] == '1' && visited[i][j] == false){
                visit(grid, i, j ,visited);
                count++;
            }
        }
    }
    return count;
}

public void visit(char[][] grid, int x, int y, boolean[][] visited) {
    int h = grid.length;
    int w = grid[0].length;
    //递归结束条件
    if (x >= h || y >= w || x < 0 || y < 0 || visited[x][y] == true || grid[x][y] == '0')
        return;
    visited[x][y] = true;
    visit(grid, x + 1, y, visited);
    visit(grid, x - 1, y, visited);
    visit(grid, x, y + 1, visited);
    visit(grid, x, y - 1, visited);
    return;
}