542. 01 Matrix

class Solution {
public:
    vector<vector<int>> res;
    int m, n;
    vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
        m = matrix.size(); if (m == 0)  return res;
        n = matrix[0].size(); if (n==0) return res;
        res = vector<vector<int>>(m, vector<int>(n, INT_MAX));
        queue<pair<int,int>> q;
        static int dirs[] = { -1, 0, 1, 0, -1 };
        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});
                }
        while (!q.empty()) {
            auto p = q.front();
            q.pop();
            for (int k = 0; k < 4; k++) {
                int x = p.first + dirs[k];
                int y = p.second + dirs[k+1];
                if (x < 0 || y < 0 || x >= m || y >= n || res[p.first][p.second] + 1 >= res[x][y]) continue;
                res[x][y] = res[p.first][p.second] + 1;
                q.push({x, y});
            }
        }
        return res;
    }
};

/* DFS: slow
class Solution {
public:
    vector<vector<int>> res;
    int m, n;
    vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
        m = matrix.size(); if (m == 0)  return res;
        n = matrix[0].size(); if (n==0) return res;
        res = vector<vector<int>>(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;
                    dfs(matrix, i, j);
                }
        return res;
    }
    void dfs(vector<vector<int>>& mx, int i, int j) {
        static int dirs[] = { -1, 0, 1, 0, -1 };
        for (int d = 0; d < 4; d++)
        {
            int x = i + dirs[d];
            int y = j + dirs[d+1];
            if (x < 0 || x >= m || y < 0 || y >= n || res[i][j] + 1 >= res[x][y]) continue;
            res[x][y] = res[i][j] + 1;
            dfs(mx, x, y);
        }
    }
};
*/

/* each point BFS: too slow
class Solution {
public:
    vector<vector<int>> res;
    int m, n;
    vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
        m = matrix.size(); if (m == 0)  return res;
        n = matrix[0].size(); if (n==0) return res;
        res = vector<vector<int>>(m, vector<int>(n, INT_MAX));
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                helper(matrix, i, j);
        return res;
    }
    void helper(vector<vector<int>>& mx, int i, int j) {
        static int dirs[] = { -1, 0, 1, 0, -1 };
        if (mx[i][j] == 0) {
            res[i][j] = 0;
            return;
        }
        vector<vector<bool>> v(m, vector<bool>(n, false));
        queue<pair<int,int>> q;
        q.push({i,j});
        v[i][j] = true;
        int lv = 0;
        while (!q.empty()) {
            int qs = q.size();
            lv++;
            while (qs-- > 0) {
                auto p = q.front();
                q.pop();
                for (int k = 0; k < 4; k++) {
                    int x = p.first + dirs[k];
                    int y = p.second + dirs[k+1];
                    if (x < 0 || y < 0 || x >= m || y >= n || v[x][y]) continue;
                    if (mx[x][y] == 0) {
                        res[i][j] = lv;
                        return;
                    }
                    else {
                        q.push({x, y});
                        v[x][y] = true;
                    }
                }
            }
        }
    }
};
*/

 

posted @ 2018-12-06 16:04  JTechRoad  阅读(169)  评论(0编辑  收藏  举报