当前位置:网站首页>LeetCode 542、01 矩阵
LeetCode 542、01 矩阵
2022-04-23 20:23:00 【亡于灬】
542、01 矩阵
1)题目描述
给定一个由 0
和 1
组成的矩阵 mat
,请输出一个大小相同的矩阵,其中每一个格子是 mat
中对应位置元素到最近的 0
的距离。
两个相邻元素间的距离为 1
。
示例 1:
输入:mat = [[0,0,0],[0,1,0],[0,0,0]]
输出:[[0,0,0],[0,1,0],[0,0,0]]
示例 2:
输入:mat = [[0,0,0],[0,1,0],[1,1,1]]
输出:[[0,0,0],[0,1,0],[1,2,1]]
提示:
m == mat.length
n == mat[i].length
1 <= m, n <= 10^4
1 <= m * n <= 10^4
mat[i][j] is either 0 or 1.
mat
中至少有一个0
2)分析
广度优先搜索。
存储所有 0 的位置,入队,然后遍历队列,如果四周存在 1 ,那么设置步数,然后添加进队列。
3)C++
代码
class Solution {
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {
int m=mat.size();
int n=mat[0].size();
vector<vector<int>> seen(m,vector<int> (n,0));
vector<vector<int>> distance(m,vector<int> (n,0));
queue<pair<int,int>> que;
int dx[4]={
1,0,-1,0};
int dy[4]={
0,1,0,-1};
//访问所有的零并将0加入队列
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(!mat[i][j]){
que.emplace(i,j);
seen[i][j]=1;
}
}
}
//广度优先搜索
while(!que.empty()){
int x=que.front().first;
int y=que.front().second;
que.pop();
for(int i=0;i<4;i++){
int newX=x+dx[i];
int newY=y+dy[i];
if(newX>=0&&newX<m&&newY>=0&&newY<n&&!seen[newX][newY]){
seen[newX][newY]=1;
distance[newX][newY]=distance[x][y]+1;
que.emplace(newX,newY);
}
}
}
return distance;
}
};
版权声明
本文为[亡于灬]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_38342510/article/details/124360368
边栏推荐
- 【PTA】L1-002 打印沙漏
- [latex] 5 how to quickly write out the latex formula corresponding to the formula
- Analysis of the relationship between generalized Bim and CAD under the current background
- Tencent Qiu Dongyang: techniques and ways of accelerating deep model reasoning
- DTMF双音多频信号仿真演示系统
- R语言ggplot2可视化:ggplot2可视化散点图并使用geom_mark_ellipse函数在数据簇或数据分组的数据点周围添加椭圆进行注释
- [talkative cloud native] load balancing - the passenger flow of small restaurants has increased
- LeetCode动态规划训练营(1~5天)
- How does onlyoffice solve no route to host
- How do BIM swindlers cheat? (turn)
猜你喜欢
Numpy mathematical function & logical function
考研英语唐叔的语法课笔记
Five minutes to show you what JWT is
Plato Farm元宇宙IEO上线四大,链上交易颇高
Recommend an open source free drawing software draw IO exportable vector graph
Es keyword sorting error reason = fielddata is disabled on text fields by default Set fielddata = true on keyword in order
Mathematical modeling column | Part 5: MATLAB optimization model solving method (Part I): Standard Model
SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
堡垒机、跳板机JumpServer的搭建,以及使用,图文详细
Actual measurement of automatic ticket grabbing script of barley network based on selenium (the first part of the new year)
随机推荐
Implementation of mypromise
[talkative cloud native] load balancing - the passenger flow of small restaurants has increased
一. js的深拷贝和浅拷贝
PCL点云处理之直线与平面的交点计算(五十三)
Modeling based on catiav6
Commit and rollback in DCL of 16 MySQL
Sqoop imports tinyint type fields to boolean type
R language uses the preprocess function of caret package for data preprocessing: BoxCox transform all data columns (convert non normal distribution data columns to normal distribution data and can not
Thirty What are VM and VC?
Scripy tutorial - (2) write a simple crawler
star
R language uses econocrats package to create microeconomic or macroeconomic map, visualize indifference function indifference curve, customize calculation intersection, and customize the parameters of
Installation and use of NVM
AQS learning
DNS cloud school | analysis of hidden tunnel attacks in the hidden corner of DNS
R语言survival包coxph函数构建cox回归模型、ggrisk包ggrisk函数和two_scatter函数可视化Cox回归的风险评分图、解读风险评分图、基于LIRI数据集(基因数据集)
Click an EL checkbox to select all questions
Remote code execution in Win 11 using wpad / PAC and JScript 3
Development of Matlab GUI bridge auxiliary Designer (functional introduction)
Alicloud: could not connect to SMTP host: SMTP 163.com, port: 25