输入:只包含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. Hadoop学习笔记二

    一.设置无密码sudo权限,不用在普通用户和root用户间来回切换 chmod u+w /etc/sudoers vim /etc/sudoers #首行添加如下的内容: hadoop ALL=(ro ...

  2. Spring整合JDBC及事务处理

    1.Spring整合JDBC DAO是数据访问对象(data access object)的简写.接口是实现松耦合的关键,Spring也鼓励使用接口,但不是强制的. 捕获异常时希望能尝试从异常状态中恢 ...

  3. JMX与Spring

    1.什么是JMX JMX可以监控类的运行情况,可以在程序运行期查看并修改类属性的信息. 举一个应用实例:在一个系统中常常会有一些配置信息,比如服务的IP地址,端口号什么的,那么如何来写这些代码呢? 下 ...

  4. SDN第二次作业

    作业链接 阅读文章<软件定义网络(SDN)研究进展>,并根据所阅读的文章,书写一篇博客,回答以下问题(至少3个): 为什么需要SDN?SDN特点? 随着网络规模的不断扩大,封闭的网络设备内 ...

  5. Hibernate之HelloWorld

    1. 步骤 0. 导入相关Jar包. 1. 编写Hibernate的持久化文件 (默认为hibernate.cfg.xml). 2. 编写持久化类. 3. 创建对象 - 关系文件(.htm.xml文件 ...

  6. 安装RabbitMQ(一)

    RabbitMQ简介 RabbitMQ 是由 LShift 提供的一个 Advanced Message Queuing Protocol (AMQP) 的开源实现,由以高性能.健壮以及可伸缩性出名的 ...

  7. install atom markdown preview plus error

    Installing "markdown-preview-enhanced@0.15.2" failed.Hide output- npm ERR! Darwin 17.2.0 n ...

  8. ipset批量配置iptables

    简介: ipset是iptables的扩展,允许你创建匹配整个地址sets(地址集合)的规则.而不像普通的iptables链是线性的存储和过滤,ip集合存储在带索引的数据结构中,这种集合比较大也可以进 ...

  9. C#泛型简单应用

    最近老板要在app里开展金融模块了,产品一下就丢丢丢二三十个表单下来,怎么办,赶紧写代码,有20多个表单要提交呢,得建20多个表.等等,好像这些表单很相似,公司信息,个人信息,可是还有部分不同信息怎么 ...

  10. ubuntu命令行下java工程编辑与算法(第四版)环境配置

    ubuntu命令行下java工程编辑与算法(第四版)环境配置 java 命令行 javac java 在学习算法(第四版)中的实例时,因需要安装配套的java编译环境,可是在编译java文件的时候总是 ...