LeetCode 542. 01 Matrix
输入:只包含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的更多相关文章
- 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的值. 最 ...
- [leetcode] 542. 01 Matrix (Medium)
给予一个矩阵,矩阵有1有0,计算每一个1到0需要走几步,只能走上下左右. 解法一: 利用dp,从左上角遍历一遍,再从右下角遍历一遍,dp存储当前位置到0的最短距离. 十分粗心的搞错了col和row,改 ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- [Leetcode Week10]01 Matrix
01 Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/01-matrix/description/ Description Given a ...
- Java实现 LeetCode 542 01 矩阵(暴力大法,正反便利)
542. 01 矩阵 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . 示例 1: 输入: 0 0 0 0 1 0 0 0 0 输出: 0 0 0 ...
- Leetcode 542.01矩阵
01矩阵 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . 示例 1: 输入: 0 0 0 0 1 0 0 0 0 输出: 0 0 0 0 1 0 ...
- 542 01 Matrix 01 矩阵
给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离.两个相邻元素间的距离为 1 .示例 1:输入:0 0 00 1 00 0 0输出:0 0 00 1 00 0 0 示例 2:输入: ...
- 542. 01 Matrix
class Solution { public: vector<vector<int>> res; int m, n; vector<vector<int>& ...
- 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 ...
随机推荐
- 《深入理解Java虚拟机》学习笔记(二)
垃圾回收的前提是判断对象是否存活,对象不再存活时将会被回收,下面是2种判断的方法. 引用计数法: 主流的Java虚拟机并没有使用引用计数法来管理内存,重要的原因就是循环引用的问题难以解决. 可达性分析 ...
- BZOJ 2055: 80人环游世界 [上下界费用流]
2055: 80人环游世界 题意:n个点带权图,选出m条路径,每个点经过val[i]次,求最小花费 建图比较简单 s拆点限制流量m 一个点拆成两个,限制流量val[i],需要用上下界 图中有边的连边, ...
- Spring MVC的配置和使用
Spring MVC的配置和使用 笔记仓库:https://github.com/nnngu/LearningNotes Spring MVC需要的jar包 文章中 Spring MVC 使用的版本是 ...
- SPSS 批量添加标签
- .net 分割字符串
string a = "1-2-3-4-5-6-7-8-9"; string[] b = a.Split(new Char[] { '-' }); for (int i = 0; ...
- w !sudo tee %
w !sudo tee % 该命令可用于保存有权限的写文件
- Yii的URL助手
Url 帮助类 获得通用 URL 记住 URLs 检查相对 URLs Url 帮助类提供一系列的静态方法来帮助管理 URL. 获得通用 URL 有两种获取通用 URLS 的方法 :当前请求的 home ...
- MysqL自动提交机制的关闭
MysqL在执行一句数据库操作命令的时候,通常都是自动提交的.常用引擎下有两种,分别是MyIsam和InnoDB,MyIsam是不支持事务处理的,但InnoDB支持,但InnoDB在不开启事务处理的情 ...
- yaf框架加载全局公共函数
在Boostrap里面建一个方法(按规则命名的函数都会被自动执行) public function _initCommonFunctions(){ Yaf_Loader::import(Yaf_App ...
- DxPackNet 2.视频截图和捕捉帧图片
在上一节的基础上 打开了摄像头后: 1.视频截图------调用 CatchBmp 方法即可获取当前帧的 bmp 图像, //调用截屏函数 获取当前图片 Bitmap bmp = camCaptur ...