当前位置:网站首页>[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;
}
}
边栏推荐
- [content of content type request header]
- CorelDRAW 2022中文精简64位直装版下载
- C # generates PPK files in putty format (supports passphrase)
- QT web development - VIDEO - Notes
- int和位数组互转
- QML控件类型:ToolTip
- 手机上怎么开户?还有,在线开户安全么?
- Pytoch -- foundation refers to the north_ II. What high school students can understand [back propagation and gradient descent]
- VirtualBox 安装增强功能
- 我的PMP学习考试心得
猜你喜欢

修复表中的名字(首字符大写,其他小写)

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

LabVIEW calculates the camera image sensor resolution and lens focal length
![Pytorch —— 基础指北_贰 高中生都能看懂的[反向传播和梯度下降]](/img/6e/279dbb7a8d7a5ecd240de464c5b8b2.png)
Pytorch —— 基础指北_贰 高中生都能看懂的[反向传播和梯度下降]

In the fourth week of June, the list - flying melon data up main growth ranking list (BiliBili platform) was released!

5款主流智能音箱入门款测评:苹果小米华为天猫小度,谁的表现更胜一筹?

LabVIEW计算相机图像传感器分辨率以及镜头焦距

What other hot spots are hidden under 1500W playback? Station B 2 future trends you can't miss

【JS】【掘金】获取关注了里不在关注者里的人

机器学习9-通用逼近器径向基函数神经网络,在新观点下审视PDA和SVM
随机推荐
Upstream and downstream in software development
SWT/ANR问题--ANR/JE引发SWT
我想知道股票开户怎么开户?究竟网上开户是否安全么?
Pytoch -- foundation refers to the north_ II. What high school students can understand [back propagation and gradient descent]
(summary I) Halcon Foundation's target finding features + becoming a regular
P6773 [noi2020] destiny (DP, segment tree merging)
Alphabet rearrange inator 3000 (dictionary tree custom sorting)
CentOS installs multiple versions of PHP and switches
Batch import of Excel data in applet
House change for agricultural products? "Disguised" house purchase subsidy!
QML control type: tooltip
Is there any discount for opening an account now? In addition, is it safe to open a mobile account?
electron之坑addon
Delete duplicate email
go导入自建包
我的PMP学习考试心得
Laravel event & subscription
What is the difference between port number and process number?
AS400 大厂面试
ANR问题的分析与解决思路