zoukankan      html  css  js  c++  java
  • 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;
        }
    };
  • 相关阅读:
    Effective C++ 1.让自己习惯C++
    C++Primer 第十九章
    C++Primer 第十八章
    C++Primer 第十七章
    C++Primer 第十六章
    C++Primer 第十五章
    C++Primer 第十四章
    ~~在python中踩过的坑以及问题~~(不断更新)
    ~~Python解释器安装教程及环境变量配置~~
    ~~Py2&Py3~~
  • 原文地址:https://www.cnblogs.com/JTechRoad/p/8988382.html
Copyright © 2011-2022 走看看