当前位置:网站首页>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;
}
};
边栏推荐
- 【JetCache】JetCache的配置说明和注解属性说明
- [pulsar document] concepts and architecture
- Vulkan is not a "panacea"“
- Tensorflow 2. Chapter 15 of X (keras) source code explanation: migration learning and fine tuning
- Gan model architecture in mm
- Two common methods and steps of character device registration
- In the first half of 2022, there are 10 worth seeing, and each sentence can bring you strength!
- 【AutoSAR 一 概述】
- Wechat applet obtains the information of an element (height, width, etc.) and converts PX to rpx.
- 奥斯陆大学:Li Meng | 基于Swin-Transformer的深度强化学习
猜你喜欢
How SQLSEVER removes data with duplicate IDS
【案例分享】让新时代教育发展与“数”俱进
Automated defect analysis in electron microscopic images-论文阅读笔记
University of Toronto:Anthony Coache | 深度强化学习的条件可诱导动态风险度量
文件操作IO-Part2
Gan model architecture in mm
Use Jenkins II job
Why is the website slow to open?
Two common methods and steps of character device registration
leetcode-2280:表示一个折线图的最少线段数
随机推荐
Callback event after the antv X6 node is dragged onto the canvas (stepping on a big hole record)
【AutoSAR 八 OS】
Tensorflow 2. Chapter 15 of X (keras) source code explanation: migration learning and fine tuning
Shell 实现文件基本操作(sed-编辑、awk-匹配)
Is there a free text to speech tool to help recommend?
【案例分享】让新时代教育发展与“数”俱进
百度智能云牵头打造智能云综合标准化平台
Introduction and use of ftrace tool
[IELTS reading] Wang Xiwei reading P2 (reading fill in the blank)
File operation io-part2
Overlay of shutter (Pop-Up)
leetcode-934:最短的桥
Attributeerror: 'tuple' object has no attribute 'layer' problem solving
1.11 - bus
leetcode-224:基本计算器
Why is the website slow to open?
【JetCache】JetCache的配置说明和注解属性说明
微信小程序获取某个元素的信息(高、宽等),并将px转换为rpx。
测试右移:线上质量监控 ELK 实战
About qbytearray storage hexadecimal and hexadecimal conversion