当前位置:网站首页>Leetcode-934: the shortest Bridge
Leetcode-934: the shortest Bridge
2022-07-03 00:47:00 【Chrysanthemum headed bat】
leetcode-934: The shortest bridge
subject
In a given two-dimensional binary array A in , There are two islands .( The island is connected by four sides 1 Form a largest group .)
Now? , We can 0 Turn into 1, To connect the two islands , Become an island .
Returns the that must be flipped 0 The minimum number of .( You can guarantee that the answer is at least 1 .)
Example 1:
Input :A = [[0,1],[1,0]]
Output :1
Example 2:
Input :A = [[0,1,0],[0,0,0],[0,0,1]]
Output :2
Example 3:
Input :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]]
Output :1
Problem solving
Method 1 :BFS
1. First traverse , Find a point on one of the islands
2. Mark all the islands found in the first step as -1
3. Starting from the first island , Start BFS, But when I met 1, It means we met the second island
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();
// Find the point of one of the islands
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
// Mark one of the found islands as -1, And save it all q2 in
queue<pair<int,int>> q1;// be used for bfs
queue<pair<int,int>> q2;// Save the node , For the following bfs Initial value
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 Indicates the points that have been visited ,0 Indicates points that have not been visited ,1 Indicates the destination Island
// Find the value for 1 The dot of indicates that you have found
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;
}
};
边栏推荐
猜你喜欢
文件操作IO-Part2
University of Toronto: Anthony coach | the conditions of deep reinforcement learning can induce dynamic risk measurement
【案例分享】让新时代教育发展与“数”俱进
ftrace工具的介绍及使用
Callback event after the antv X6 node is dragged onto the canvas (stepping on a big hole record)
leetcode-2280:表示一个折线图的最少线段数
可下载《2022年中国数字化办公市场研究报告》详解1768亿元市场
Sentry developer contribution Guide - configure pycharm
Gan model architecture in mm
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
随机推荐
Vulkan并非“灵药“
AttributeError: ‘tuple‘ object has no attribute ‘layer‘问题解决
Shell implements basic file operations (SED edit, awk match)
tail -f 、tail -F、tailf的区别
Rust字符串切片、结构体和枚举类
kubernetes编写yml简单入门
【AutoSAR 十 IO架构】
1.12 - 指令
LeedCode1480. Dynamic sum of one-dimensional array
关于QByteArray存储十六进制 与十六进制互转
Vulkan practice first bullet
leetcode-934:最短的桥
mm中的GAN模型架构
helm 基础学习
Hundreds of continuous innovation to create free low code office tools
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
Shell 实现文件基本操作(切割、排序、去重)
Nacos+openfeign error reporting solution
About qbytearray storage hexadecimal and hexadecimal conversion
【luogu P4320】道路相遇(圆方树)