LeetCode 542. 01 矩阵 BFS题解 ,熟悉BFS、练习矩阵问题。

题目

542. 01 矩阵

https://leetcode.cn/problems/01-matrix/

给定一个由 0 和 1 组成的矩阵 mat ,请输出一个大小相同的矩阵,其中每一个格子是 mat 中对应位置元素到最近的 0 的距离。

两个相邻元素间的距离为 1 。

BFS 从0出发

思路

思路1: 图的多源BFS:我们从0点出发,碰到未访问的1时,mat[x][y]=前一个点的值+1

状态标记:1未访问时,可将值设置为 Integer.MIN

代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
    * 执行用时:
    * 12 ms
    * , 在所有 Java 提交中击败了
    * 61.51%
    * 的用户
    * 内存消耗:
    * 43.1 MB
    * , 在所有 Java 提交中击败了
    * 92.57%
    * 的用户
    * 通过测试用例:
    * 50 / 50
    */

/**
    * 图的多源BFS:我们从0点出发,碰到未访问的1时,mat[x][y]=前一个点的值+1
    * 1未访问时,可将值设置为 Integer.MIN
    */
public int[][] updateMatrix(int[][] mat) {
    int m = mat.length;
    int n = mat[0].length;
    int UNVISITED1 = Integer.MIN_VALUE;

    Queue<int[]> pointQ = new LinkedList<>();
    // 将未访问1的值设置好,同时将0点入队
    for (int row = 0; row < m; row++) {
        for (int col = 0; col < n; col++) {
            if (mat[row][col] == 0) {
                pointQ.offer(new int[]{row, col});
            } else {
                mat[row][col] = UNVISITED1;
            }
        }
    }

    int[][] directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

    while (!pointQ.isEmpty()) {
        int size = pointQ.size();
        for (int i = 0; i < size; i++) {
            int[] point = pointQ.poll();
            int x = point[0];
            int y = point[1];

            for (int[] direction : directions) {
                int nextX = x + direction[0];
                int nextY = y + direction[1];

                // 下个点为未访问过的1
                if (inArea(nextX, m) && inArea(nextY, n) && mat[nextX][nextY] == UNVISITED1) {
                    mat[nextX][nextY] = mat[x][y] + 1;
                    // 访问过的1入队
                    pointQ.offer(new int[]{nextX, nextY});
                }
            }
        }
    }
    return mat;
}

private boolean inArea(int x, int m) {
    return x >= 0 && x < m;
}

复杂度分析

时间

发散的时候每次新增四个节点,时间复杂度应该是O(4mn),去掉常数阶O(mn)

空间

最差情况,矩阵退化成条状,队列会维护整个矩阵,空间复杂度大致为O(mn)

BFS 从1出发

思路

先入队与0相邻的1,再对入队元素处理。

与0相邻的1,第一次入队的时候已经将距离设置为1。

后续访问新的1时,只需在前序1的距离基础上累加。

ArrayDeque 相比 LinkedList 优势:无多余node对象的创建开销;数组存储是连续的,可以利用CPU缓存行加速访问。

代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
    * bfs另一种做法,先入队与0相邻的1,再对入队元素处理
    * <p>
    * ArrayDeque 相比 LinkedList 优势:无多余node对象的创建开销;数组存储是连续的,可以利用CPU缓存行加速访问。
    * 手写难免有bug,可结合情况多debug,IDE还是比人眼更细致。
    */
public int[][] updateMatrixBFS2(int[][] mat) {
    // 这种思路下,无法使用原数组标记1的访问状态,因为从队列中先出队了与0相邻的1,如果标识为了1,那么离0更远的1之后的发散则无法触达已经访问过的1
    // 所以使用一个新的数组来维护结果
    int m = mat.length;
    int n = mat[0].length;
    int[][] res = new int[m][n];

    int[][] directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
    Queue<int[]> pointQ = new ArrayDeque<>();
    for (int row = 0; row < m; row++) {
        for (int col = 0; col < n; col++) {
            // 先入队0相邻的1点
            if (mat[row][col] == 0) {
                for (int[] direction : directions) {
                    int newX = row + direction[0];
                    int newY = col + direction[1];
                    if (inArea(newX, m) && inArea(newY, n) && mat[newX][newY] == 1 && res[newX][newY] == 0) {
                        pointQ.offer(new int[]{newX, newY});
                        // 相邻的1,距离最近0的距离就是1了
                        res[newX][newY] = 1;
                    }
                }
            }
        }
    }

    // 后面针对1点处理
    while (!pointQ.isEmpty()) {
        // 出队1
        int[] point = pointQ.poll();
        int x = point[0];
        int y = point[1];
        for (int[] direction : directions) {
            int newX = x + direction[0];
            int newY = y + direction[1];
            // 新发散的点还没被处理, res[newX][newY]==0
            if (inArea(newX, m) && inArea(newY, n) && mat[newX][newY] == 1 && res[newX][newY] == 0) {
                // 入队新的1点,继续往深层遍历
                pointQ.offer(new int[]{newX, newY});
                // 当前1点的距离在原先1到0的距离上+1
                res[newX][newY] = res[x][y] + 1;
            }
        }
    }
    return res;
}

复杂度分析

同解法1。

Ref