输入:只包含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. 洛谷 [P1198] 最大数

    首先这是一道线段树裸题,但是线段树长度不确定,那么我们可以在建树的时候,将每一个节点初始化为-INF,每次往队尾加一个元素即一次单节点更新,注意本题的数据范围,其实并不用开 long long,具体请 ...

  2. BZOJ 1185: [HNOI2007]最小矩形覆盖 [旋转卡壳]

    1185: [HNOI2007]最小矩形覆盖 Time Limit: 10 Sec  Memory Limit: 162 MBSec  Special JudgeSubmit: 1435  Solve ...

  3. SDP(5):ScalikeJDBC- JDBC-Engine:Streaming

    作为一种通用的数据库编程引擎,用Streaming来应对海量数据的处理是必备功能.同样,我们还是通过一种Context传递产生流的要求.因为StreamingContext比较简单,而且还涉及到数据抽 ...

  4. C#SMTP发邮件

    public static bool SendMailUse() { string host = "smtp.163.com";// 邮件服务器smtp.163.com表示网易邮箱 ...

  5. 小甲鱼OD学习第9讲

    这次我们的任务是破解这个要注册的软件,如下图所示 当我们输入账号密码的时候,它会提示输入的账号密码是无效的,如下图 我们把程序载入OD,然后在查找字符串那里输入提示的无效账号密码的字符串,如下图 然后 ...

  6. Windows Server 2016-部署第一台域控制器

    上节我们提到有关WinSer 2016 Active Directory域服务概述.WinSer2016 AD域中新增的功能及先决条件等,本节就为大家带来WinSer2016下搭建部署第一台域控的操作 ...

  7. java环境搭建 windows

    windows搭建Java环境 1.下载java开发工具jdk安装包 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/inde ...

  8. jQuery smartMenu右键自定义上下文菜单插件

    http://www.zhangxinxu.com/wordpress/?p=1667 <%@ page contentType="text/html; charset=UTF-8&q ...

  9. Deep Learning for Information Retrieval

    最近关注了一些Deep Learning在Information Retrieval领域的应用,得益于Deep Model在对文本的表达上展现的优势(比如RNN和CNN),我相信在IR的领域引入Dee ...

  10. EntityFramework Core 2.0执行原始查询如何防止SQL注入?

    前言 接下来一段时间我们来讲讲EntityFramework Core基础,精简的内容,深入浅出,希望为想学习EntityFramework Core的童鞋提供一点帮助. EntityFramewor ...