当前位置:网站首页>leetcode-934:最短的桥
leetcode-934:最短的桥
2022-07-02 23:55:00 【菊头蝙蝠】
题目
在给定的二维二进制数组 A 中,存在两座岛。(岛是由四面相连的 1 形成的一个最大组。)
现在,我们可以将 0 变为 1,以使两座岛连接起来,变成一座岛。
返回必须翻转的 0 的最小数目。(可以保证答案至少是 1 。)
示例 1:
输入:A = [[0,1],[1,0]]
输出:1
示例 2:
输入:A = [[0,1,0],[0,0,0],[0,0,1]]
输出:2
示例 3:
输入:A = [[1,1,1,1,1],[1,0,0,0,1],[1,0,1,0,1],[1,0,0,0,1],[1,1,1,1,1]]
输出:1
解题
方法一:BFS
1.首先遍历,找到其中岛屿的一个点
2.将第一步中找到的那个岛屿全部标记成-1
3.从第一个岛屿出发,开始BFS,一但遇到1,就说明遇到第二个岛屿了
class Solution {
public:
vector<vector<int>> dirs={
{
-1,0},{
1,0},{
0,1},{
0,-1}};
int shortestBridge(vector<vector<int>>& grid) {
int m=grid.size();
int n=grid[0].size();
//找到其中一个岛屿的点
pair<int,int> startPoint;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(grid[i][j]==1){
startPoint={
i,j};
goto OUT;
}
}
}
OUT:
//BFS
//将找到的其中一个岛屿全部标记成-1,并把它全部存入q2中
queue<pair<int,int>> q1;//用于bfs
queue<pair<int,int>> q2;//将节点存起来,用于之后的bfs初始值
grid[startPoint.first][startPoint.second]=-1;
q1.push(startPoint);
while(!q1.empty()){
auto [x,y]=q1.front();
q2.push({
x,y});
q1.pop();
for(vector<int>& dir:dirs){
int nx=x+dir[0];
int ny=y+dir[1];
if(nx>=0&&nx<m&&ny>=0&&ny<n&&grid[nx][ny]==1){
q1.push({
nx,ny});
grid[nx][ny]=-1;
}
}
}
//BFS
//-1表示已经访问过的点,0表示还未访问过的点,1表示目的岛屿
//找到值为1的点就说明找到了
int count=0;
while(!q2.empty()){
int l=q2.size();
for(int i=0;i<l;i++){
auto [x,y]=q2.front();
q2.pop();
for(vector<int>& dir:dirs){
int nx=x+dir[0];
int ny=y+dir[1];
if(nx>=0&&nx<m&&ny>=0&&ny<n){
if(grid[nx][ny]==1) return count;
else if(grid[nx][ny]==0){
q2.push({
nx,ny});
grid[nx][ny]=-1;
}
}
}
}
count++;
}
return -1;
}
};
边栏推荐
- 【雅思阅读】王希伟阅读P2(阅读填空)
- Preview word documents online
- Vulkan-实践第一弹
- Linux Software: how to install redis service
- [shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)
- Two common methods and steps of character device registration
- JSON conversion tool class
- DotNet圈里一个优秀的ORM——FreeSql
- 数组常用操作方法整理(包含es6)及详细使用
- 关于XML一些介绍和注意事项
猜你喜欢

百度智能云牵头打造智能云综合标准化平台

【AutoSAR 十一 通信相关机制】

2022上半年值得被看见的10条文案,每一句都能带给你力量!

Rust所有权(非常重要)

Use Jenkins II job

Liad: the consumer end of micro LED products is first targeted at TVs above 100 inches. At this stage, it is still difficult to enter a smaller size

Automated defect analysis in electronic microscopic images

【AutoSAR 八 OS】

Attributeerror: 'tuple' object has no attribute 'layer' problem solving

Multiprocess programming (I): basic concepts
随机推荐
FAQ | FAQ for building applications for large screen devices
Attributeerror: 'tuple' object has no attribute 'layer' problem solving
Pageoffice - bug modification journey
About the practice topic of screen related to unity screen, unity moves around a certain point inside
百度智能云牵头打造智能云综合标准化平台
微信小程序获取某个元素的信息(高、宽等),并将px转换为rpx。
NC50528 滑动窗口
Nc20806 District interval
【AutoSAR 十二 模式管理】
线程的启动与优先级
Explain in detail the significance of the contour topology matrix obtained by using the contour detection function findcontours() of OpenCV, and how to draw the contour topology map with the contour t
【AutoSAR 四 BSW概述】
免费自媒体必备工具分享
helm 基础学习
NC50965 Largest Rectangle in a Histogram
Hundreds of continuous innovation to create free low code office tools
Use Jenkins II job
【AutoSAR 十一 通信相关机制】
Teach you JDBC hand in hand -- structure separation
[jetcache] jetcache configuration description and annotation attribute description