当前位置:网站首页>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;
}
};
边栏推荐
- Rust字符串切片、结构体和枚举类
- node_ Modules cannot be deleted
- lex && yacc && bison && flex 配置的问题
- 【案例分享】让新时代教育发展与“数”俱进
- mysql 多表联合删除
- The "2022 China Digital Office Market Research Report" can be downloaded to explain the 176.8 billion yuan market in detail
- 【luogu P4320】道路相遇(圆方树)
- 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
- leetcode-2280:表示一个折线图的最少线段数
- About qbytearray storage hexadecimal and hexadecimal conversion
猜你喜欢
【AutoSAR 八 OS】
Introduction of UART, RS232, RS485, I2C and SPI
【AutoSAR 五 方法论】
Linux软件:如何安装Redis服务
1.12 - Instructions
Hdu3507 (slope DP entry)
Two common methods and steps of character device registration
Vulkan practice first bullet
In the first half of 2022, there are 10 worth seeing, and each sentence can bring you strength!
pod生命周期详解
随机推荐
Automated defect analysis in electronic microscopic images
File operation io-part2
MySQL 23 classic interview hanging interviewer
Multi process programming (III): message queue
Solution to the problem of abnormal display of PDF exported Chinese documents of confluence
Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)
Callback event after the antv X6 node is dragged onto the canvas (stepping on a big hole record)
[applet project development -- JD mall] user defined search component of uni app (middle) -- search suggestions
Nc17059 queue Q
Briefly talk about other uses of operation and maintenance monitoring
Basic use of shell script
腾讯云免费SSL证书扩展文件含义
【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(中)-- 搜索建议
【日常训练】871. 最低加油次数
lex && yacc && bison && flex 配置的問題
奥斯陆大学:Li Meng | 基于Swin-Transformer的深度强化学习
University of Oslo: Li Meng | deep reinforcement learning based on swing transformer
leetcode-241:为运算表达式设计优先级
mm中的GAN模型架构
node_ Modules cannot be deleted