当前位置:网站首页>[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;
}
}
边栏推荐
- What is the difference between port number and process number?
- Mathematical knowledge: 01 sequence satisfying conditions - find combinatorial number
- Batch import of Excel data in applet
- halcon数组的一些使用
- House change for agricultural products? "Disguised" house purchase subsidy!
- 2022年最新csdn涨薪技术栈-app自动化测试概述
- What is PMP?
- SWT / anr problem - SWT caused by too long dump time
- 最新微信ipad协议 CODE获取 公众号授权等
- What are the preferential activities for stock account opening? In addition, is it safe to open a mobile account?
猜你喜欢
(总结一)Halcon基础之寻找目标特征+转正
5款主流智能音箱入门款测评:苹果小米华为天猫小度,谁的表现更胜一筹?
(翻译)实时内联验证更容易让用户犯错的原因
How to realize the scene linkage of intelligent lock, lamp and intelligent curtain motor in zhiting?
Machine learning 10 belief Bayesian classifier
What other hot spots are hidden under 1500W playback? Station B 2 future trends you can't miss
Pytorch - - Basic Reference North Deux élèves du secondaire peuvent comprendre [Rétropropagation et Gradient descendant]
CorelDRAW 2022中文精简64位直装版下载
CentOS installs multiple versions of PHP and switches
centos 安装多个版本的php并切换
随机推荐
[JS] [Nuggets] get people who are not followers
端口号和进程号的区别?
Clickhouse 消除由group by产生的间隙
Mathematical knowledge: 01 sequence satisfying conditions - find combinatorial number
The latest CSDN salary increase technology stack in 2022 overview of APP automated testing
SWT / anr problem - storagemanagerservice stuck
What is project management?
小程序云开发之--微信公众号文章采集篇
Live shopping mall source code, realize left-right linkage of commodity classification pages
nacos配置中心使用教程
删除重复的电子邮箱
[无线通信基础-15]:图解移动通信技术与应用发展-3- 数字通信2G GSM、CDMA、3G WDCMA/CDMA200/TD-SCDMA、4G LTE、5G NR概述
FL studio20.9 fruit software advanced Chinese edition electronic music arrangement
How do I open an account on my mobile phone? Also, is it safe to open an account online?
2022年最新csdn涨薪技术栈-app自动化测试概述
P6773 [noi2020] destiny (DP, segment tree merging)
Ernie-gram, 显式、完备的 n-gram 掩码语言模型,实现了显式的 n-gram 语义单元知识建模。
运算符重载的初识
Ernie gram, an explicit and complete n-gram mask language model, implements explicit n-gram semantic unit knowledge modeling.
@ConfigurationProperties和@Value的区别