【leet-code】542. 01 矩阵
题目描述
给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离。
两个相邻元素间的距离为 1 。
示例 1:
输入:
0 0 0
0 1 0
0 0 0
输出:
0 0 0
0 1 0
0 0 0
示例 2:
输入:
0 0 0
0 1 0
1 1 1
输出:
0 0 0
0 1 0
1 2 1
注意:
- 给定矩阵的元素个数不超过 10000。
- 给定矩阵中至少有一个元素是 0。
- 矩阵中的元素只在四个方向上相邻: 上、下、左、右。
算法
可以用动态规划或者BFS,如果用DFS有超时的风险。
BFS
- 遍历matrix矩阵,将所有\(matrix[i][j]=0\)的位置信息\((i, j)\)保存到队列中,所有\(matrix[i][j]=1\)的将值从1改到定义的无穷大。
- 当队列不为空的时候,持续循环:
- 从队列中弹出一个元素,获取位置信息,将该位置(i,j)的上下左右做个判断,如果某个位置上的值大于matrix[i][j]+1,那么将该值改为matrix[i][j]+1,重新压入队列
- 进行改值操作的时候注意边界条件
BFS代码
#define INF 10000
class Solution {
vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
int row = matrix.size();
if (row == 0)
return matrix;
int col = matrix[0].size();
queue<pair<int, int> > myQueue;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (matrix[i][j] == 0)
myQueue.push(pair<int, int>(i, j));
else
matrix[i][j] = INF;
}
}
while (!myQueue.empty())
{
pair<int, int> rec = myQueue.front();
myQueue.pop();
if (rec.first-1 >= 0 && matrix[rec.first-1][rec.second] > matrix[rec.first][rec.second]+1)
{
matrix[rec.first-1][rec.second] = matrix[rec.first][rec.second]+1;
myQueue.push(pair<int, int>(rec.first-1, rec.second));
}
if (rec.second-1 >= 0 && matrix[rec.first][rec.second-1] > matrix[rec.first][rec.second]+1)
{
matrix[rec.first][rec.second-1] = matrix[rec.first][rec.second]+1;
myQueue.push(pair<int, int>(rec.first, rec.second-1));
}
if (rec.first+1 < row && matrix[rec.first+1][rec.second] > matrix[rec.first][rec.second]+1)
{
matrix[rec.first+1][rec.second] = matrix[rec.first][rec.second]+1;
myQueue.push(pair<int, int>(rec.first+1, rec.second));
}
if (rec.second+1 < col && matrix[rec.first][rec.second+1] > matrix[rec.first][rec.second]+1)
{
matrix[rec.first][rec.second+1] = matrix[rec.first][rec.second]+1;
myQueue.push(pair<int, int>(rec.first, rec.second+1));
}
}
return matrix;
}
动态规划
基本的思想就是遍历matrix,如果matrix[i][j]是0的话,dp[i][j]直接为0,否则,它与四周的dp值有关,又因为从左上到右下的更新过程中,只能确定左上角两边的dp值;
所以要再从右下角往左上角遍历,这样就把dp[i][j]四周的dp值都考虑进去了。
动态规划代码
class Solution {
vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
// 两次遍历,左上到右下和右下到左上,更新完的dp就是最终的答案
int row = matrix.size();
if (row == 0)
return matrix;
int col = matrix[0].size();
vector<vector<int> > dp(row, vector<int>(col));
if (matrix[0][0] == 0)
dp[0][0] = 0;
else
dp[0][0] = INF;
// 第一次
for (int i = 1; i < col; i++)
{
if (matrix[0][i] == 0)
dp[0][i] = 0;
else
dp[0][i] = min(INF, dp[0][i-1] + 1);
}
for (int i = 1; i < row; i++)
{
if (matrix[i][0] == 0)
dp[i][0] = 0;
else
dp[i][0] = min(INF, dp[i-1][0] + 1);
}
for (int i = 1; i < row; i++)
{
for (int j = 1; j < col; ++j)
{
if (matrix[i][j] == 0)
dp[i][j] = 0;
else
dp[i][j] = min(INF, min(dp[i-1][j], dp[i][j-1]) + 1);
}
}
// 打印第一次更新成果
// for (int i = 0; i < row; i++)
// {
// for (int j = 0; j < col; j++)
// cout << dp[i][j] << ' ';
// cout << endl;
// }
//
// cout << "===========我是分割线===========" << endl;
// 第二次
for (int i = col - 2; i >= 0; i--)
{
if (matrix[row-1][i] == 0)
dp[row-1][i] = 0;
else
dp[row-1][i] = min(dp[row-1][i], dp[row-1][i+1] + 1);
}
for (int i = row - 2; i >= 0; i--)
{
if (matrix[i][col-1] == 0)
dp[i][col-1] = 0;
else
dp[i][col-1] = min(dp[i][col-1], dp[i+1][col-1] + 1);
}
for (int i = row - 2; i >= 0; i--)
{
for (int j = col - 2; j >= 0; j--)
{
if (matrix[i][j] == 1)
dp[i][j] = min(dp[i][j], min(dp[i+1][j], dp[i][j+1]) + 1);
}
}
return dp;
}
};
【leet-code】542. 01 矩阵的更多相关文章
- 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 ...
- 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 ...
- Leetcode 542:01 矩阵 01
Leetcode 542:01 矩阵 01 Matrix### 题目: 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . Given a matr ...
- 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的值. 最 ...
- [EOJ Monthly 2018.10][C. 痛苦的 01 矩阵]
题目链接:C. 痛苦的 01 矩阵 题目大意:原题说的很清楚了,不需要简化_(:з」∠)_ 题解:设\(r_i\)为第\(i\)行中0的个数,\(c_j\)为第\(j\)列中0的个数,\(f_{i,j ...
- 【Leet Code】Palindrome Number
Palindrome Number Total Accepted: 19369 Total Submissions: 66673My Submissions Determine whether an ...
- MATLAB小函数:将列向量转化为0-1矩阵
MATLAB小函数:将列向量转化为0-1矩阵 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 将列向量转化为0-1矩阵,例如 A = 1 2 1 5 3 ...
- Leet Code 771.宝石与石头
Leet Code编程题 希望能从现在开始,有空就做一些题,自己的编程能力太差了. 771 宝石与石头 简单题 应该用集合来做 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S ...
随机推荐
- oracle 索引移动到不同的分区
最近系统空间不够,要进行数据库清理,truncate数据之后,发现数据不连续,导致这个表空间占用巨大,想过使用shrink.move.但是shrink得效率比较慢,选择了move.语句大概如此: SE ...
- 20个Linux防火墙应用技巧
转载 1.显示防火墙的状态 以root权限运行下面的命令: # iptables -L -n -v 参数说明: -L:列出规则. -v:显示详细信息.此选项会显示接口名称.规则选项和TOS掩码,以及封 ...
- 用pyspider爬取并解析json字符串
获取堆糖网站所有用户的id 昵称及主页地址 #!/usr/bin/env python # -*- encoding: utf-8 -*- # Created on 2016-06-21 13:57: ...
- 【adb】执行adb devices 设备offline
解决办法: 1.执行adb kill-server,在执行adb devices 2.重启手机 ---------------------------------------------------- ...
- program--历史故事
- 了解一下Ubuntu系统
百度百科: ubuntu系统基于Debian发行版和GNOME桌面环境.Ubuntu的目标在于为一般用户提供一个最新的.同时又相当稳定的主要由自由软件构建而成的操作系统,它可免费使用,并带有社团及专业 ...
- FPGA中带优先级的if else if与不带优先级的case的探讨
我们知道在书本上都说让我们尽量使用不带优先级的的数据选择器,今天我们就来探讨一下二者的区别. 例子1:带优先级的的数据选择器,综合成功,且没有任何警告. module detection_prio # ...
- 深度学习Tensorflow生产环境部署(上·环境准备篇)
最近在研究Tensorflow Serving生产环境部署,尤其是在做服务器GPU环境部署时,遇到了不少坑.特意总结一下,当做前车之鉴. 1 系统背景 系统是ubuntu16.04 ubuntu@ub ...
- 原来你离BAT只有一步之遥
ladies and乡亲们 喜迎全民嗨购双11 i春秋准备搞一波大优惠 优惠力度有多大 跨店凑单满400-50? 指定商品199减100? 史无钜惠 不凑单 不指定 一次直降9000元 原价:2580 ...
- Laravel 5.6: Specified key was too long error
Laravel 5.6: Specified key was too long error 在Laravel执行以下命令: php artisan migrate 这是由于Laravel5.6设置了数 ...