zoukankan      html  css  js  c++  java
  • 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;
                        }
                    }
                }
            }
        }
    };
    */
  • 相关阅读:
    swagger,参数,list,swaggerui测试list<string>类型参数示例
    c# 获取路径的方法详解
    The assembly for System.Buffers could not be loaded;无法加载System.Buffers的程序集
    把.net Core 项目迁移到VS2019 for MAC
    Asp.net Core 3.0 Identity 使用smtp账户确认和密码恢复
    Mui使用jquery并且使用点击跳转新窗口
    运行第一个abp项目VS2015+localDB
    Asp.net MVC+Bootstrap3的悬浮式登录框效果
    MVC配置ckeditor+ckfinder
    EF 数据初始化
  • 原文地址:https://www.cnblogs.com/JTechRoad/p/10077224.html
Copyright © 2011-2022 走看看