zoukankan      html  css  js  c++  java
  • [Leetcode Week10]01 Matrix

    01 Matrix 题解

    原创文章,拒绝转载

    题目来源:https://leetcode.com/problems/01-matrix/description/


    Description

    Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.

    The distance between two adjacent cells is 1.

    Example 1:

    Input:

    0 0 0
    0 1 0
    0 0 0
    

    Output:

    0 0 0
    0 1 0
    0 0 0
    

    Example 2:

    Input:

    0 0 0
    0 1 0
    1 1 1
    

    Output:

    0 0 0
    0 1 0
    1 2 1
    

    Solution

    class Solution {
    private:
        struct vertex
        {
            int r, c;
            vertex(int _r, int _c) : r(_r), c(_c) {}
        };
        int row, col;
        vector<vector<int> > res;
    public:
        bool isValid(int r, int c) {
            return r >= 0 && r < row && c >= 0 && c < col;
        }
    
        void insertQ(queue<vertex>& q, int r, int c, int val) {
            if (!isValid(r, c))
                return;
            if (res[r][c] == -1) {
                res[r][c] = val + 1;
                q.push(vertex(r, c));
            } else if (res[r][c] > val + 1) {
                res[r][c] = val + 1;
            }
        }
    
        vector<vector<int> > updateMatrix(vector<vector<int> >& A) {
            this -> row = A.size();
            this -> col = A[0].size();
            vector<int> rowvec(col, -1);
            vector<vector<int> > resRef(row, rowvec);
            this -> res = resRef;
            int i, j;
            queue<vertex> q;
            for (i = 0; i < row; i++) {
                for (j = 0; j < col; j++) {
                    if (A[i][j] == 0) {
                        res[i][j] = 0;
                        q.push(vertex(i, j));
                    }
                }
            }
    
            while (!q.empty()) {
                vertex v = q.front();
                q.pop();
                int val = res[v.r][v.c];
                insertQ(q, v.r + 1, v.c, val);
                insertQ(q, v.r - 1, v.c, val);
                insertQ(q, v.r, v.c + 1, val);
                insertQ(q, v.r, v.c - 1, val);
            }
            return res;
        }
    };
    

    解题描述

    这道题是典型的搜索类问题,我采用了BFS,从为0的顶点开始,逐步更新临近圈层的步数直到矩阵中所有的点的步数都计算出来。

  • 相关阅读:
    为什么Redis比Memcached易
    请注意CSDN社区微通道,许多其他的精彩等着你
    采用ACE登录设施(一)HelloWorld
    AIX 7.1 install python
    spring mvc入门
    iOS开展——全球应对MotionEvent
    2015第35周日
    2015第35周六转相见恨晚的知识列表
    2015第35周五JavaScript变量
    2015第35周四
  • 原文地址:https://www.cnblogs.com/yanhewu/p/7822775.html
Copyright © 2011-2022 走看看