当前位置:网站首页>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.lengthn == mat[i].length1 <= m, n <= 10^41 <= m * n <= 10^4mat[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
边栏推荐
- Es error: request contains unrecognized parameter [ignore_throttled]
- Local call feign interface message 404
- SIGIR'22 "Microsoft" CTR estimation: using context information to promote feature representation learning
- Research on open source OCR engine
- 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
- 上海回应“面粉官网是非法网站”:疏于运维被“黑”,警方已立案
- 【问题解决】‘ascii‘ codec can‘t encode characters in position xx-xx: ordinal not in range(128)
- Automatically fill in body temperature and win10 task plan
- 论文写作 19: 会议论文与期刊论文的区别
- 三十.什么是vm和vc?
猜你喜欢

Numpy - creation of data type and array

Customize timeline component styles

Es keyword sorting error reason = fielddata is disabled on text fields by default Set fielddata = true on keyword in order

【目标跟踪】基于帧差法结合卡尔曼滤波实现行人姿态识别附matlab代码

Numpy mathematical function & logical function

RT-1052学习笔记 - GPIO架构分析

Numpy Index & slice & iteration
![Es error: request contains unrecognized parameter [ignore_throttled]](/img/17/9131c3eb023b94b3e06b0e1a56a461.png)
Es error: request contains unrecognized parameter [ignore_throttled]

上海回應“面粉官網是非法網站”:疏於運維被“黑”,警方已立案

上海回应“面粉官网是非法网站”:疏于运维被“黑”,警方已立案
随机推荐
Development of Matlab GUI bridge auxiliary Designer (functional introduction)
An error is reported when sqoop imports data from Mysql to HDFS: sqlexception in nextkeyvalue
考研英语唐叔的语法课笔记
The market share of the financial industry exceeds 50%, and zdns has built a solid foundation for the financial technology network
On BIM data redundancy theory
[target tracking] pedestrian attitude recognition based on frame difference method combined with Kalman filter, with matlab code
6-5 字符串 - 2. 字符串复制(赋值) (10 分)C语言标准函数库中包括 strcpy 函数,用于字符串复制(赋值)。作为练习,我们自己编写一个功能与之相同的函数。
Fundamentals of network communication (LAN, Wan, IP address, port number, protocol, encapsulation and distribution)
[graph theory brush question-5] Li Kou 1971 Find out if there is a path in the graph
【PTA】L2-011 玩转二叉树
微信中金财富高端专区安全吗,证券如何开户呢
Error reported by Azkaban: Azkaban jobExecutor. utils. process. ProcessFailureException: Process exited with code 64
DNS cloud school | quickly locate DNS resolution exceptions and keep these four DNS status codes in mind
After route link navigation, the sub page does not display the navigation style problem
How does onlyoffice solve no route to host
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
Handwritten Google's first generation distributed computing framework MapReduce
Computing the intersection of two planes in PCL point cloud processing (51)
go-zero框架数据库方面避坑指南
ArcGIS JS version military landmark drawing (dovetail arrow, pincer arrow, assembly area) fan and other custom graphics