输入:只包含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. vue-router 二级路由

    /** * Created by 我 on 2017/12/4. */ import Vue from 'vue' //import导入 Vue(自己起的名) from 从 vue import Vu ...

  2. python基础dict,集合,文件

    字典是一种key:value的数据类型dict1{ 'stud1':'孙礼昭', 'stud2':'slz', 'stud3':'sunlizhao',}dict是无序的,key是唯一的  天生去重增 ...

  3. I can do it

    If it doesn't challange you ,it won't change you.

  4. asp.net core 中 sql server 2017 数据库连接测试

    使用sql server 2017 进行连接: 配置appsettings.json文件 { "ConnectionStrings": { "DefaultConnect ...

  5. Angular20 nginx安装,angular项目部署

    1 nginx安装(Windows版本) 1.1 下载安装包 到官网下载Windows版本的nginx安装包 技巧01:下载好的压缩包解压即可,无需安装 1.2 启动nginx 进入到解压目录,点击 ...

  6. php.ini 中文详解

    [PHP]  ; PHP还是一个不断发展的工具,其功能还在不断地删减  ; 而php.ini的设置更改可以反映出相当的变化,  ; 在使用新的PHP版本前,研究一下php.ini会有好处的   ;;; ...

  7. Windows 定时任务对数据库进行操作

    定时对数据库进行操作可以用mysql的event事件来完成,但是只有mysql5.1后的才支持,所以有一定的局限性,也可以通过其他的mysql管理软件实现,而我发现Windows本身就有个定时任务的功 ...

  8. Java并发系列[5]----ReentrantLock源码分析

    在Java5.0之前,协调对共享对象的访问可以使用的机制只有synchronized和volatile.我们知道synchronized关键字实现了内置锁,而volatile关键字保证了多线程的内存可 ...

  9. ionic2+Angular 组件(多个组件)浅谈

    第一步,新建组件: ionic g component product-img-list 命令执行成功之后项目中生成的文件: 第二步:生成文件解析: ①product-img-list.ts impo ...

  10. git 版本控制的简单应用

    一.通过 honebrew 安装git , 教程参考:http://brew.sh/index_zh-cn.html 也可对比参考:http://book.51cto.com/art/201107/2 ...