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的更多相关文章

  1. 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的值. 最 ...

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

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

  3. LeetCode 542. 01 Matrix

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

  4. 542 01 Matrix 01 矩阵

    给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离.两个相邻元素间的距离为 1 .示例 1:输入:0 0 00 1 00 0 0输出:0 0 00 1 00 0 0 示例 2:输入: ...

  5. Java实现 LeetCode 542 01 矩阵(暴力大法,正反便利)

    542. 01 矩阵 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . 示例 1: 输入: 0 0 0 0 1 0 0 0 0 输出: 0 0 0 ...

  6. [Leetcode Week10]01 Matrix

    01 Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/01-matrix/description/ Description Given a ...

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

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

  8. hdu 01 Matrix

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

  9. 【LeetCode】01 Matrix 解题报告

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

随机推荐

  1. Spark Executor内幕彻底解密:Executor工作原理图、ExecutorBackend注册源码解密、Executor实例化内幕、Executor具体工作内幕

    本课主题 Spark Executor 工作原理图 ExecutorBackend 注册源码鉴赏和 Executor 实例化内幕 Executor 具体是如何工作的 Spark Executor 工作 ...

  2. io问题导致的insert缓慢

    生产环境一日常insert在业务量加倍之后非常缓慢,对有问题sql做awr报告和10046trace得出以下结论:In the 10046 trace, the execution of sql el ...

  3. February 6 2017 Week 6 Monday

    There are no shortcuts to any place worth going. 任何值得去的地方,都没有捷径. Several years ago, I climbed the Hu ...

  4. 「hihocoder1413 Rikka with String」

    题目 哈哈哈哈哈哈哈哈哈哈我还没自闭 好像前后调了两天了 哈哈哈哈哈哈哈哈哈哈我还没自闭 这道题就是给定一个小写字母串,回答分别把每个位置上的字符替换为\(#\)后的本质不同的子串数 首先就是跨过这个 ...

  5. Thread-Specific-Storage for C/C++

    引用出处:https://www.cse.wustl.edu/~schmidt/PDF/TSS-pattern.pdf 摘要: 理论上多线程会提高程序性能,但实际上,由于在获取和释放锁的开销,多线程经 ...

  6. PHP----练习-----新闻管理----增删改查

    练习-----新闻管理 题目要求如下: 做法: [1]建数据库 [2]封装类文件--------DBDA.class.php <?php class DBDA { public $fuwuqi= ...

  7. 小BAT解决大麻烦_某卡教室控制软件

    @echo off mode con cols= lines= if "%1" == "h" goto begin mshta vbscript:)(windo ...

  8. 轻量ORM-SqlRepoEx (八)MySQL、Sql Service 迁移

    数据库变更在编程应用中是常的,MySQL.Sql Service之间的数据迁移更为常见,在 SqlRepoEx2.0DemoForAspCore中演示了,这种数据库之间切换时SqlRepoEx是如何的 ...

  9. Linux -- 用户组篇

    Linux -- 用户与用户组 1.Linux 系统中有三种角色:所有者(用户),用户组与其他人,一张图可以说明用户与用户组的关系. 如图,某公司相当于一个用户组,该用户组下有A,B两个用户,用户拥有 ...

  10. Oracle 缓存命中率问题一则(里面有个问题咨询大佬们)

    近日,核心数据库频繁抱出数据库缓存命中率过低,于是开始进行排查. 1.监控软件告警信息 2.抓取告警时间段内的awr报告进行分析 3.execute与parse命中率过低,说明分析(硬解析与软解析)的 ...