当前位置:网站首页>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
边栏推荐
- JDBC tool class jdbcconutil gets the connection to the database
- Redis cache penetration, cache breakdown, cache avalanche
- 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
- ArcGIS js api 4. X submergence analysis and water submergence analysis
- Don't bother tensorflow learning notes (10-12) -- Constructing a simple neural network and its visualization
- Remote code execution in Win 11 using wpad / PAC and JScript 1
- Sqoop imports tinyint type fields to boolean type
- On BIM data redundancy theory
- PCA based geometric feature calculation of PCL point cloud processing (52)
- Matlab analytic hierarchy process to quickly calculate the weight
猜你喜欢
Sqoop imports tinyint type fields to boolean type
Automatically fill in body temperature and win10 task plan
Matlab analytic hierarchy process to quickly calculate the weight
Three. Based on ply format point cloud voxel model JS upload interface writing
【栈和队列专题】—— 滑动窗口
波场DAO新物种下场,USDD如何破局稳定币市场?
Development of Matlab GUI bridge auxiliary Designer (functional introduction)
DTMF双音多频信号仿真演示系统
Shanghai a répondu que « le site officiel de la farine est illégal »: l'exploitation et l'entretien négligents ont été « noirs » et la police a déposé une plainte
Mathematical modeling column | Part 5: MATLAB optimization model solving method (Part I): Standard Model
随机推荐
Investigate why close is required after sqlsession is used in mybatties
A useless confession artifact
【PTA】L1-002 打印沙漏
Es error: request contains unrecognized parameter [ignore_throttled]
Use the rolling division method to find the maximum common divisor of two numbers
NC basic usage 3
Customize timeline component styles
Error reported by Azkaban: Azkaban jobExecutor. utils. process. ProcessFailureException: Process exited with code 127
Actual measurement of automatic ticket grabbing script of barley network based on selenium (the first part of the new year)
[target tracking] pedestrian attitude recognition based on frame difference method combined with Kalman filter, with matlab code
star
Why does ES6 need to introduce map when JS already has object type
Installation and use of NVM
SQL Server Connectors By Thread Pool | DTSQLServerTP 插件使用说明
Computing the intersection of two planes in PCL point cloud processing (51)
Leetcode dynamic planning training camp (1-5 days)
JDBC tool class jdbcconutil gets the connection to the database
R language ggplot2 visualization: ggplot2 visualizes the scatter diagram and uses geom_ mark_ The ellipse function adds ellipses around data points of data clusters or data groups for annotation
SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
Zdns was invited to attend the annual conference of Tencent cloud basic resources and share the 2020 domain name industry development report