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的顶点开始,逐步更新临近圈层的步数直到矩阵中所有的点的步数都计算出来。

[Leetcode Week10]01 Matrix的更多相关文章

  1. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  2. [leetcode] 542. 01 Matrix (Medium)

    给予一个矩阵,矩阵有1有0,计算每一个1到0需要走几步,只能走上下左右. 解法一: 利用dp,从左上角遍历一遍,再从右下角遍历一遍,dp存储当前位置到0的最短距离. 十分粗心的搞错了col和row,改 ...

  3. leetcode 542. 01 Matrix 、663. Walls and Gates(lintcode) 、773. Sliding Puzzle 、803. Shortest Distance from All Buildings

    542. 01 Matrix https://www.cnblogs.com/grandyang/p/6602288.html 将所有的1置为INT_MAX,然后用所有的0去更新原本位置为1的值. 最 ...

  4. LeetCode 542. 01 Matrix

    输入:只包含0,1的矩阵 输出:元素1到达最近0的距离 算法思想:广度优先搜索. 元素为0为可达区域,元素为1为不可达区域,我们的目标是为了从可达区域不断地扩展至不可达区域,在扩展的过程中,也就计算出 ...

  5. Java for LeetCode 059 Spiral Matrix II

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  6. 计算机学院大学生程序设计竞赛(2015’12)01 Matrix

    01 Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  7. [LeetCode] 59. Spiral Matrix II 螺旋矩阵 II

    Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order. For ...

  8. hdu 01 Matrix

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission ...

  9. [LeetCode] 01 Matrix 零一矩阵

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

随机推荐

  1. Kotlin怎样使用Android的Dagger2

    作者:Antonio Leiva 时间:Apr 11, 2017 原文链接:https://antonioleiva.com/dagger-android-kotlin/ 在Android上,创建去耦 ...

  2. 用Python 的一些用法与 JS 进行类比,看有什么相似?

    Python 是一门运用很广泛的语言,自动化脚本.爬虫,甚至在深度学习领域也都有 Python 的身影.作为一名前端开发者,也了解 ES6 中的很多特性借鉴自 Python (比如默认参数.解构赋值. ...

  3. Python全栈 MongoDB 数据库(概念、安装、创建数据)

    什么是关系型数据库?           是建立在关系数据库模型基础上的数据库,借助于集合代数等概念和方法来处理数据库中的数据,             同时也是一个被组织成一组拥有正式描述性的表格( ...

  4. LeetCode 215——数组中的第 K 个最大元素

    1. 题目 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: 输入: [3,2,1,5,6,4] 和 k = 2 ...

  5. DFS实现模板

    以如下图的无向图G4为例,进行图的深度优先搜索: 假设从顶点v1出发进行搜索,在访问了顶点v1之后,选择邻接点v2.因为v2未曾访问,则从v2出发进行搜索.依次类推,接着从v4 .v8 .v5出发进行 ...

  6. 剑指offer:正则表达式匹配

    目录 题目 解题思路 具体代码 题目 题目链接 剑指offer:正则表达式匹配 题目描述 请实现一个函数用来匹配包括'.'和'*'的正则表达式.模式中的字符.表示任意一个字符,而*表示它前面的字符可以 ...

  7. penLDAP学习笔记

    LDAP协议 目录是一组具有类似属性.以一定逻辑和层次组合的信息.常见的例子是通讯簿,由以字母顺序排列的名字.地址和电话号码组成.目录服务是一种在分布式环境中发现目标的方法.目录具有两个主要组成部分: ...

  8. lintcode-113-删除排序链表中的重复数字 II

    113-删除排序链表中的重复数字 II 给定一个排序链表,删除所有重复的元素只留下原链表中没有重复的元素. 样例 给出 1->2->3->3->4->4->5-&g ...

  9. [bzoj] 2038 小Z的袜子(hose) || 莫队

    原题 给出一个序列,求给定[l,r]内有任意取两个数,有多大概率是一样的 简单的莫队,每次+-当前区间里有的这个颜色的袜子的个数,最后除以(r-l+1)*(r-l)/2即可. 记得约分. #inclu ...

  10. CDQZ 2017 游记

    Day0: 提前放了一整天假,颓过去了.老吕让我去给B层的讲课,ppt还没做,只能在飞机上赶了QAQ.然后从上午到了衡水就一直在路上或者天上,到了晚上才到学校,然而ppt还是没有做完.还有,鄂尔多斯真 ...