# 542. 01 Matrix

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-MeIkJgUOie5y4gfq9UA%2F-MeJaiffZOgbgH3bW8Ay%2Fimage.png?alt=media\&token=d96b5343-17b3-4219-af01-218777bb617c)

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-MeIkJgUOie5y4gfq9UA%2F-MeJald5gqEgTC-Nc3fI%2Fimage.png?alt=media\&token=1d880068-a009-49bd-84c0-50849d0d2fe7)

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-MjK64Z7SYvzW2rfxmie%2F-MjKCXrHJSfxmM1Wlv-u%2Fimage.png?alt=media\&token=b9e87e4a-037b-474d-8c5a-46409115f9eb)

## follow template style

time: O(mn)

space: O(mn)

```java
class Solution {
    private static final int DIRECTIONS[][] = {{0,1}, {1, 0}, {0, -1}, {-1,0}};

    public int[][] updateMatrix(int[][] mat) {
        // BFS from 0 to find 1, record dist in res, run all queue level++
        int m = mat.length, n = mat[0].length;
        int[][] res = new int[m][n];
        
        boolean vistited[][] = new boolean[m][n];
        Queue<int[]> queue = new LinkedList<>();
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (mat[i][j] == 0) {
                    queue.offer(new int[]{i, j}); // 00 01 02 10 12
                    vistited[i][j] = true;
                }
            }
        }
        int dist = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int q = 0; q < size; q++) {
                int cell[] = queue.poll();
                int i = cell[0];
                int j = cell[1];
                if (mat[i][j] == 1) {
                    res[i][j] = dist;
                }
                for (int dir[] : DIRECTIONS) {
                    int x = i + dir[0];
                    int y = j + dir[1];
                    if (x >= 0 && x < m && y >=0 && y < n && !vistited[x][y]) {
                        queue.offer(new int[]{x, y});
                        vistited[x][y] = true;
                    }
                }
            }
            dist++;
        }
        return res;
    }
}
```



## Use some tricks

use MAX to represent 1 and not visited, update 1's value to dist (but I think it's not different)

just save some space, time is the same

```java
class Solution {
    private static final int DIRECTIONS[][] = {{0,1}, {1, 0}, {0, -1}, {-1,0}};

    public int[][] updateMatrix(int[][] mat) {
        // BFS from 0 to find 1, put 0 into queue, 1 mark MAX, so when meet MAX, if new node (MAX) > prenode+1
        // update new node = prenode+1
        int m = mat.length, n = mat[0].length;

        Queue<int[]> queue = new LinkedList<>();
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (mat[i][j] == 0) {
                    queue.offer(new int[]{i, j}); // 00 01 02 10 12
                } else {
                    mat[i][j] = Integer.MAX_VALUE;
                }
            }
        }
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int q = 0; q < size; q++) {
                int cell[] = queue.poll();
                int i = cell[0];
                int j = cell[1];
                for (int dir[] : DIRECTIONS) {
                    int x = i + dir[0];
                    int y = j + dir[1];
                    if (x >= 0 && x < m && y >=0 && y < n && mat[x][y] > mat[i][j] + 1) {
                        queue.offer(new int[]{x, y});
                        mat[x][y] = mat[i][j] + 1;
                    }
                }
            }
        }
        return mat;
    }
}
```



## latest one:

```java
/**
use 0 as start, when find 1, we find the dist in "this BFS" (so it's O(mn))

but if we use 1 as start, when find 0 -> it's hard to fill answer in this BFS
-> become you have to run many times BFS to get dist
 */
```

```java
class Solution {
    private record Step(int x, int y, int level){};
    private static final int[][] DIRECTIONS = {{0,1}, {1,0}, {0,-1}, {-1,0}};
    
    public int[][] updateMatrix(int[][] mat) {
        int m = mat.length;
        int n = mat[0].length;
        Queue<Step> queue = new LinkedList<>();
        boolean[][] visited = new boolean[m][n];

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (mat[i][j] == 0) {
                    queue.offer(new Step(i, j, 0));
                }
            }
        }

        while (!queue.isEmpty()) {
            Step cur = queue.poll();
            if (cur.x < 0 || cur.x >= m || cur.y < 0 || cur.y >= n || visited[cur.x][cur.y]) {
                continue;
            }
    
            visited[cur.x][cur.y] = true;
            if (mat[cur.x][cur.y] == 1) {
                mat[cur.x][cur.y] = cur.level;
            }
            for (int[] dir : DIRECTIONS) {
                queue.offer(new Step(cur.x + dir[0], cur.y + dir[1], cur.level+1));
            }
        }
        return mat;
    }
}

/**

 */
```
