[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 ...
随机推荐
- Django 运行Admin 页面时出现 UnicodeDecodeError: 'gbk' codec can't decode byte XXXX解决方法
具体报错信息 Traceback (most recent call last): File "D:\Anaconda3\lib\site-packages\django\core\hand ...
- spring与mybatis整合(基于配置文件)
本文主要介绍了如何将mybatis和spring整合在一起使用,本人使用的是mybatis3.05 + spring3.1.0M2 ,使用dbcp作为数据库连接池. 1.编写数据访问接口(UserDa ...
- POJ 2182 / HDU 2711 Lost Cows(平衡树)
Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular di ...
- C++STL——list
一.相关定义 list 链表,分配的内存不连续 可以高效地进行插入/删除元素 不可随机访问,访问速度慢 特征 只能通过迭代器来访问list中的元素 在头和尾都可以插入元素 二.list [前提条件] ...
- 第十一次ScrumMeeting会议
第十一次ScrumMeeting 时间:2017/11/18 4:00-4:30 地点:主203 人员:全体人员 照片: 工作情况 名字 今日计划 明天的工作 遇到的困难 蔡帜 讨论策划详情\确定WB ...
- ASP.NET页面之间传值Session(2)
想必这个肯定是大家使用中最常见的用法了,其操作与Application类似,作用于用户个人,所以,过量的存储会导致服务器内存资源的耗尽. 优点:1.使用简单,不仅能传递简单数据类型,还能传递对象. 2 ...
- 51nod 1967路径定向(欧拉回路)
题目大意:给出一个图,安排边的方向,使得入度等于出度的点数最多,并给出方案. 首先假设是个无向图,不妨认定偶点必定可以满足条件 我们还会发现,奇点的个数必定是偶数个 那么如果把奇点两两用辅助边连起来, ...
- [洛谷P4208][JSOI2008]最小生成树计数
题目大意:有$n$个点和$m$条边(最多有$10$条边边权相同),求最小生成树个数 题解:对于所有最小生成树,每种边权的边数是一样的.于是就可以求出每种边权在最小生成树中的个数,枚举这种边的边集,求出 ...
- [Leetcode] Anagrams 颠倒字母构成词
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- 使用 URLDecoder 和 URLEncoder 对中文字符进行编码和解码
原文: https://blog.csdn.net/justloveyou_/article/details/57156039 使用 URLDecoder 和 URLEncoder 对中文字符进行编码 ...