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 == ) return res;
n = matrix[].size(); if (n==) return res;
res = vector<vector<int>>(m, vector<int>(n, INT_MAX));
queue<pair<int,int>> q;
static int dirs[] = { -, , , , - };
for (int i = ; i < m; i++)
for (int j = ; j < n; j++)
if (matrix[i][j] == ) {
res[i][j] = ;
q.push({i,j});
}
while (!q.empty()) {
auto p = q.front();
q.pop();
for (int k = ; k < ; k++) {
int x = p.first + dirs[k];
int y = p.second + dirs[k+];
if (x < || y < || x >= m || y >= n || res[p.first][p.second] + >= res[x][y]) continue;
res[x][y] = res[p.first][p.second] + ;
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;
}
}
}
}
}
};
*/
542. 01 Matrix的更多相关文章
- 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 (Medium)
给予一个矩阵,矩阵有1有0,计算每一个1到0需要走几步,只能走上下左右. 解法一: 利用dp,从左上角遍历一遍,再从右下角遍历一遍,dp存储当前位置到0的最短距离. 十分粗心的搞错了col和row,改 ...
- LeetCode 542. 01 Matrix
输入:只包含0,1的矩阵 输出:元素1到达最近0的距离 算法思想:广度优先搜索. 元素为0为可达区域,元素为1为不可达区域,我们的目标是为了从可达区域不断地扩展至不可达区域,在扩展的过程中,也就计算出 ...
- 542 01 Matrix 01 矩阵
给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离.两个相邻元素间的距离为 1 .示例 1:输入:0 0 00 1 00 0 0输出:0 0 00 1 00 0 0 示例 2:输入: ...
- Java实现 LeetCode 542 01 矩阵(暴力大法,正反便利)
542. 01 矩阵 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . 示例 1: 输入: 0 0 0 0 1 0 0 0 0 输出: 0 0 0 ...
- [Leetcode Week10]01 Matrix
01 Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/01-matrix/description/ Description Given a ...
- 计算机学院大学生程序设计竞赛(2015’12)01 Matrix
01 Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- hdu 01 Matrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
随机推荐
- SQL-SERVER学习(二) 数据表的存储过程
在C语言的程序设计中,会把一个重复使用的功能提取出来,做成一个的函数,这样就可以减少冗余代码,且更方便维护.调用.在面向对象的设计语言中,会把一个重复使用的功能提取出来,做成一个类,同样也是为了减少冗 ...
- C# 转换运算符:implicit(隐式),explicit(显示)
//A类 class Cls1 { public string name; //构造函数 public Cls1(string name) { this.name = name; } //implic ...
- BUG Review:关于getting 'android:xxx' attribute: attribute is not a string value的问题及解决方法
我们在使用Android Studio开发完应用程序后,都要将打好的apk安装包上传到各大应用市场,但是有时候上传时应用市场会出现提交的安装包不能通过应用市场的aapt解析而被打回的情况. 他们使用a ...
- MQ7.5以后的权限问题解决
MQ7.5以后权限是个问题,目前我也没有什么特别好的解决办法,把认证通道关闭就可以正常使用. 下面是IBM 官方的解释,可惜我没调通,望高人指点! 疑问 您使用MQ 7.1或者7.5创建了一个新的队列 ...
- CAShapeLayer使用
UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; [self.view addSubv ...
- 第三篇——第二部分——第一文 SQL Server镜像简单介绍
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/DBA_Huangzj/article/details/26951563 原文出处:http://bl ...
- Struts2注解 及 约定优于配置
Struts2注解 1 Struts2注解的作用 使用注解可以用来替换struts.xml配置文件!!! 2 导包 必须导入struts2-convention-plugin-2.3.15.jar包, ...
- python SQL注入测试脚本(更新中)
import requests import json import warnings warnings.filterwarnings("ignore") url = 'https ...
- 2018 Multi-University Training Contest 4 Problem B. Harvest of Apples 【莫队+排列组合+逆元预处理技巧】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6333 Problem B. Harvest of Apples Time Limit: 4000/200 ...
- springboot之静态资源路径配置
静态资源路径是指系统可以直接访问的路径,且路径下的所有文件均可被用户直接读取. 在Springboot中默认的静态资源路径有:classpath:/META-INF/resources/,classp ...