542. 01 Matrix

Posted The Tech Road

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了542. 01 Matrix相关的知识,希望对你有一定的参考价值。

https://leetcode.com/problems/01-matrix/description/

Use BFS to search. First I tried to BFS every 0, the problem is each 1 may need to be visited for multiple times to get the minimal (but not necessarily the original 1) distance. So how to differentiate the original 1 and modified 1 is a question, and another question is how to mark a spot is visisted for current 0 BFS. So I use a modified matrix (all 1 becomes INT_MAX), but this will time out for some case, like [ [0,1],[0,1],...[0,1] ], the first 0 BFS will go all way down, then next 0 the same.

Next I tried to put all 0s to the first level for BFS, then BFS from all these nodes, and solve this problem.

class Solution {
public:
    int m, n;
    vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
        m = matrix.size();
        n = matrix[0].size();
        vector<vector<int>> res(m, vector<int>(n, INT_MAX));
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                if (matrix[i][j] == 0) {
                    res[i][j] = 0;
                    bfs(res, i, j);
                }
        return res;
    }
    void bfs(vector<vector<int>>& res, int i, int j) {
        queue<pair<int,int>> q;
        q.push( {i, j} );
        int level = 0;
        while (!q.empty()) {
            level++;
            int qsz = q.size();
            while (qsz-- > 0) {
                int dirs[] = { -1, 0, 1, 0, -1 };
                
                int curx = q.front().first;
                int cury = q.front().second;
                q.pop();
                
                for (int d = 0; d < 4; d++) {
                    int x = curx + dirs[d];
                    int y = cury + dirs[d+1];
                    if (x >= 0 && x < m && y >= 0 && y < n && res[x][y] > level) {
                        res[x][y]  = min(res[x][y], level);
                        q.push( {x, y} );
                    }
                }
            }
        }
    }
};

 

class Solution {
public:
    int m, n;
    vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
        m = matrix.size();
        n = matrix[0].size();
        vector<vector<int>> res(m, vector<int>(n, INT_MAX));
        queue<pair<int,int>> q;
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                if (matrix[i][j] == 0) {
                    res[i][j] = 0;
                    q.push( {i, j} );
                }
        
        int level = 0;
        while (!q.empty()) {
            level++;
            int qsz = q.size();
            while (qsz-- > 0) {
                int dirs[] = { -1, 0, 1, 0, -1 };
                
                int curx = q.front().first;
                int cury = q.front().second;
                q.pop();
                
                for (int d = 0; d < 4; d++) {
                    int x = curx + dirs[d];
                    int y = cury + dirs[d+1];
                    if (x >= 0 && x < m && y >= 0 && y < n && res[x][y] > level) {
                        res[x][y]  = min(res[x][y], level);
                        q.push( {x, y} );
                    }
                }
            }
        }
        
        return res;
    }
};

 

以上是关于542. 01 Matrix的主要内容,如果未能解决你的问题,请参考以下文章

542. 01 Matrix

542. 01 Matrix

LeetCode 542. 01 Matrix

leetcode542 01 Matrix

542. 01 Matrix

542. 01 Matrix