[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的顶点开始,逐步更新临近圈层的步数直到矩阵中所有的点的步数都计算出来。
[Leetcode Week10]01 Matrix的更多相关文章
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- [leetcode] 542. 01 Matrix (Medium)
给予一个矩阵,矩阵有1有0,计算每一个1到0需要走几步,只能走上下左右. 解法一: 利用dp,从左上角遍历一遍,再从右下角遍历一遍,dp存储当前位置到0的最短距离. 十分粗心的搞错了col和row,改 ...
- 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的值. 最 ...
- LeetCode 542. 01 Matrix
输入:只包含0,1的矩阵 输出:元素1到达最近0的距离 算法思想:广度优先搜索. 元素为0为可达区域,元素为1为不可达区域,我们的目标是为了从可达区域不断地扩展至不可达区域,在扩展的过程中,也就计算出 ...
- 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 ...
- 计算机学院大学生程序设计竞赛(2015’12)01 Matrix
01 Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- [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 ...
- hdu 01 Matrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...
- [LeetCode] 01 Matrix 零一矩阵
Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell. The distance b ...
随机推荐
- 国际电话区号SQL
CREATE TABLE `phone_prefix` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `country` varchar(30) N ...
- Vm-Ubuntu下配置Qt开发环境
在昨天的Ubuntu换降下,安装Qt发现编译的时候是缺少opengl的 奈何找了好多方式都无法安装opengl 今天看到另一位大神写的,才发下自己找的还是有问题 大神帖子网址:http://blog. ...
- [USACO18DEC]Cowpatibility(容斥 or bitset优化暴力)
题面 题意: 给出n个五元组(一个五元组的五个数互不相同),我们称两个五元组不和谐,当且仅当任意元素都不相同,求有多少对五元组不和谐. \(Solution:\) 很容易想到 Ans = 总共对数-和 ...
- Daily Scrum02 11.30
纵然编译大作业压顶,大家还是顶住压力,保证了软工项目的持续进行. Member Today's Task Tomorrow's Task 李孟 Task856 熟悉单元测试方法熟悉单元测试方法 Tas ...
- winform 根据两点求出线上所有点及画出这条线
找出所有点: 根据斜率按照一个方向递增,求出对应的另一个方向的整数值. Point pStart = new Point(0, 2); Point pEnd = new Point(8, 2); // ...
- ArcGIS API for JavaScript使用中出现的BUG(1)
本人在使用ArcGIS API for JavaScript开发一个地图的搜索框时,总是出现一个BUG.如图所示: 搜索框总是出不来. 该引用的也引用了,找了半天终于解决,是因为路径没有定义详细. 应 ...
- mac tomcat: error 1 operation not permitted
在用微信传一个tomcat压缩包到别人后,解压后无法用idea启动,提示 error 1 operation not permitted,但是直接用命令可以执行. 仔细查看,原来是因为mac出于安全考 ...
- 关于网站转码(SiteApp转码)
1.Siteapp页面转码的意义?在百度移动搜索引擎中为更好满足用户信息需求,会同时为用户提供pc网页和mobile网页,但目前大多数PC页在移动终端中直接浏览的体验较差(交互.兼容和流量等).因此为 ...
- [LG1886]滑动窗口 单调队列
---题面--- 题解: 观察数据范围,这应该是一个复杂度O(n)的题.以最大值为例,考虑单调队列,维护一个单调递减的队列.从前向后扫,每次答案取队首,如果后面进入的比前面大,那么就弹出前面的数,因为 ...
- 【BZOJ 2503】相框 图论+讨论
这道题目就是考验了一下图论基本知识与对可爱的代码实现的应对能力. 我们先分析题干信息.我们要形成相框,那么所有的点的度为2(参与的点),那么所有度大于2的点都需要熔断,而且一次完成所有关于这个点的熔断 ...