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;
        }
    };
  • 相关阅读:
    无缘无故出现IDEA无法引入包和类的问题?
    JDK8新特性——Optional
    【工具类】ExcelUtils
    【Mybatis-Plus学习笔记(二)】查询
    【Mybatis-Plus学习笔记(一)】入门
    【Mybatis学习笔记】目录
    【超详细全过程】DataGrip的安装与使用
    【超详细全过程】python环境配置+编辑器安装(python+pycharm)
    【Mybatis工具(五)】雪花算法
    【Mybatis工具(四)】UUID实现原理及其Java实现
  • 原文地址:https://www.cnblogs.com/JTechRoad/p/8988382.html
Copyright © 2011-2022 走看看