输入:只包含0,1的矩阵

输出:元素1到达最近0的距离

算法思想:广度优先搜索。

元素为0为可达区域,元素为1为不可达区域,我们的目标是为了从可达区域不断地扩展至不可达区域,在扩展的过程中,也就计算出了这些不可达区域到达最近可达区域的距离。

每个可达元素都记录了到当前位置的距离,因此在后续的遍历中,如果是经由当前节点到达的下一节点,这个距离会被累加。

 #include <iostream>
#include <vector>
#include <queue>
using namespace std; class Solution {
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
int nRow = matrix.size();
int nCol = matrix[].size();
vector<vector<int>> answer(nRow, vector<int>(nCol));
queue<pair<int, int>> reachable;
for (int i = ; i < nRow; i++)
{
for (int j = ; j < nCol; j++)
{
if (matrix[i][j] == ) // reachable
{
reachable.push(make_pair(i, j));
answer[i][j] = ;
}
else
answer[i][j] = INT_MAX;
}
} vector<pair<int, int>> dir = vector<pair<int, int>>({make_pair(-,),make_pair(,),make_pair(,-),make_pair(,)}); while (!reachable.empty())
{
pair<int, int> cur = reachable.front();
for (int i = ; i < ; i++)
{
int x = dir[i].first;
int y = dir[i].second;
int cx = cur.first;
int cy = cur.second;
if (cx + x < || cx + x > nRow - || cy + y < || cy + y > nCol - ) // boundary test
continue;
if (matrix[cx+x][cy+y] == ) // not visited
{
matrix[cx + x][cy + y] = ; // label visited
answer[cx + x][cy + y] = answer[cx][cy] + ;
reachable.push(make_pair(cx + x, cy + y));
}
}
reachable.pop();
}
return answer;
}
};

LeetCode 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】01 Matrix 解题报告

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

  4. [Leetcode Week10]01 Matrix

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

  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 542.01矩阵

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

  7. 542 01 Matrix 01 矩阵

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

  8. 542. 01 Matrix

    class Solution { public: vector<vector<int>> res; int m, n; vector<vector<int>& ...

  9. LeetCode——542. 01 矩阵

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

随机推荐

  1. DNA序列局部比对(Smith–Waterman algorithm)

    生物信息原理作业第三弹:DNA序列局部比对,利用Smith–Waterman算法,python3.6代码实现. 实例以及原理均来自https://en.wikipedia.org/wiki/Smith ...

  2. 使用 RxJS 实现一个简易的仿 Elm 架构应用

    使用 RxJS 实现一个简易的仿 Elm 架构应用 标签(空格分隔): 前端 什么是 Elm 架构 Elm 架构是一种使用 Elm 语言编写 Web 前端应用的简单架构,在代码模块化.代码重用以及测试 ...

  3. ES6,Array.fill()函数的用法

    ES6为Array增加了fill()函数,使用制定的元素填充数组,其实就是用默认内容初始化数组. 该函数有三个参数. arr.fill(value, start, end) value:填充值. st ...

  4. java 实现websocket的两种方式

    简单说明 1.两种方式,一种使用tomcat的websocket实现,一种使用spring的websocket 2.tomcat的方式需要tomcat 7.x,JEE7的支持. 3.spring与we ...

  5. js小技巧:数组去重

    JavaScript 数组中去除重复的数据 var arr = [1, 2, 2, 3, '1', null, 'a', 'b', 'a']; var t = {}; var result = arr ...

  6. 搭建SS服务器

    体验: http://ss.ishadowx.com/ centos7 安装shadowsocks客户端 http://blog.csdn.net/guyan0319/article/details/ ...

  7. 置换群、Burnside引理与等价类计数问题

    置换群.Burnside引理与等价类计数问题 标签: 置换群 Burnside引理 置换 说说我对置换的理解,其实就是把一个排列变成另外一个排列.简单来说就是一一映射.而置换群就是置换的集合. 比如\ ...

  8. 解决java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext这个问题

    今天在运行别人的SSH项目时,遇到了这个问题 严重: Exception sending context initialized event to listener instance of class ...

  9. es2015及es2017对我们的编程方式造成了什么影响?

    记一些写代码中用得到的es6+语法,至于什么正则的拓展,数组的什么fill方法,对我们来说用处不大,就不提及了. 还有es6的import模块和class模块,这些在各种框架中都有体现,而且语法简单, ...

  10. 在Mac下配置Maven环境

    下载Maven安装文件,(http://maven.apache.org/download.html)如:apache-maven-3.5.0-bin.zip,然后解压到本地目录. 打开 .bash_ ...