当前位置:网站首页>[multi source BFS] 934 Shortest Bridge
[multi source BFS] 934 Shortest Bridge
2022-07-01 02:13:00 【Twilight_ years】
You are given an n x n binary matrix grid where 1 represents land and 0 represents water.
An island is a 4-directionally connected group of 1's not connected to any other 1's. There are exactly two islands in grid.
You may change 0's to 1's to connect the two islands to form one island.
Return the smallest number of 0's you must flip to connect the two islands.
The question : Given 01 The grid contains two contains 1 The connected component of , Find the shortest path connecting the two connected components
Ideas : Add one of the connected components to the queue , And then use Multi-source BFS solve
use dfs Add one of the connected components to the queue , And put 1 become 0( Grid class dfs)
class Solution {
final int INF=0X3f3f3f3f;
int[] dx={-1,0,1,0},dy={0,1,0,-1};
int[][] g,d;
Queue<Node>q=new LinkedList<>();
class Node{
int x,y;
public Node(){}
public Node(int x,int y){
this.x=x;
this.y=y;
}
}
public int shortestBridge(int[][] grid){
g=grid;
d=new int[g.length][g[0].length];
for(int i=0;i<d.length;i++){
Arrays.fill(d[i],0x3f3f3f3f);
}
for(int i=0;i<g.length;i++){
for(int j=0;j<g[0].length;j++){
if(g[i][j]==1){
dfs(i,j);
return bfs();
}
}
}
return -1;
}
void dfs(int x,int y){
g[x][y]=2;
d[x][y]=0;
q.add(new Node(x,y));
for(int i=0;i<4;i++){
int a=x+dx[i];
int b=y+dy[i];
if(a<0||a>=g.length||b<0||b>=g[0].length||g[a][b]!=1){
continue;
}
dfs(a,b);
}
}
int bfs(){
while(!q.isEmpty()){
Node t=q.poll();
for(int i=0;i<4;i++){
int x=t.x+dx[i];
int y=t.y+dy[i];
if(x<0||x>=g.length||y<0||y>=g[0].length||d[x][y]<=d[t.x][t.y]+1)
continue;
d[x][y]=d[t.x][t.y]+1;
if(g[x][y]==1)return d[x][y]-1;
q.add(new Node(x,y));
}
}
return -1;
}
}
边栏推荐
- QT web development - VIDEO - Notes
- Leetcode 面试题 17.10. 主要元素
- Leetcode(524)——通过删除字母匹配到字典里最长单词
- RocketQA:通过跨批次负采样(cross-batch negatives)、去噪的强负例采样(denoised hard negative sampling)与数据增强(data augment
- ANR问题的分析与解决思路
- AS400 大厂面试
- Batch import of Excel data in applet
- Clickhouse eliminates the gap caused by group by
- SWT / anr problem - SWT caused by long execution time of native method
- QML control type: tooltip
猜你喜欢

Calculate special bonus

Selenium classic interview question - multi window switching solution

Delete duplicate email

如何在智汀中实现智能锁与灯、智能窗帘电机场景联动?

Fix names in the table (first character uppercase, other lowercase)

PMP是什么?

There is no future to be expected. It is just the last fantasy of a migrant worker before he dies

QML控件类型:ToolTip

How does the property send a text message to the owner?

Video tutorial | Chang'an chain launched a series of video tutorial collections (Introduction)
随机推荐
软件开发中的上游和下游
[fundamentals of wireless communication-15]: illustrated mobile communication technology and application development-3-overview of digital communication 2G GSM, CDMA, 3G wdcma/cdma200/td-scdma, 4G LTE
2022年最新csdn涨薪技术栈-app自动化测试概述
聚焦绿色低碳,数据中心散热进入“智能冷却”新时代
CorelDRAW 2022 Chinese Simplified 64 bit direct download
哪有什么未来可期,不过是打工人临死前最后的幻想罢了
Do you write API documents or code first?
Leetcode interview question 17.10 Main elements
FL studio20.9 fruit software advanced Chinese edition electronic music arrangement
SWT / anr problem - anr/je causes SWT
What is the difference between port number and process number?
FL Studio20.9水果软件高级中文版电音编曲
(总结一)Halcon基础之寻找目标特征+转正
[Agora] user management
Ernie-gram, 显式、完备的 n-gram 掩码语言模型,实现了显式的 n-gram 语义单元知识建模。
(翻译)实时内联验证更容易让用户犯错的原因
How to maintain efficient collaboration in remote office and achieve stable growth of projects | community essay solicitation
最新微信ipad协议 CODE获取 公众号授权等
Electron pit Addon
What other hot spots are hidden under 1500W playback? Station B 2 future trends you can't miss