当前位置:网站首页>934.最短的桥(广度优先搜索)
934.最短的桥(广度优先搜索)
2022-07-30 05:38:00 【Linke66】

解题思路
方法1:暴力法
使用深度优先搜索,讲两个岛屿的坐标存储到两个数组中,然后用两个for遍历数组,找到最小置。
很暴力,时间和空间复杂度也很高。
class Solution {
public:
int shortestBridge(vector<vector<int>>& grid) {
int m=grid.size(),n=grid[0].size();
vector<vector<bool>>visited(m,vector<bool>(n,false));
vector<vector<int>>island1_axis,island2_axis;
int whichIsland=1;
for(int i=0;i<m;++i){
for(int j=0;j<n;++j){
if(!visited[i][j]&&grid[i][j]==1){
if(whichIsland==1){
++whichIsland;
dfs(grid,visited,island1_axis,i,j);
continue;
}
if(whichIsland==2){
++whichIsland;
dfs(grid,visited,island2_axis,i,j);
}
}
}
}
int min_cnt=INT_MAX;
int cnt;
for(auto &axis1:island1_axis){
int x1=axis1[0],y1=axis1[1];
for(auto &axis2:island2_axis){
int x2=axis2[0],y2=axis2[1];
cnt=abs(x1-x2)+abs(y1-y2)-1;
if(cnt<min_cnt) min_cnt=cnt;
}
}
return min_cnt;
}
void dfs(const vector<vector<int>> &grid,vector<vector<bool>> &visited,
vector<vector<int>> &axis,int x,int y)
{
if(x>=0&&x<grid.size()&&y>=0&&y<grid[0].size()
&&!visited[x][y]&&grid[x][y]==1){
visited[x][y]=true;
axis.push_back({
x,y});
dfs(grid,visited,axis,x-1,y);
dfs(grid,visited,axis,x+1,y);
dfs(grid,visited,axis,x,y-1);
dfs(grid,visited,axis,x,y+1);
}
}
};
法2:广度优先搜索
处理最短路径问题,使用广度优先搜索方法。
这个方法是看答案的,感觉很巧妙,以前没有想过这种方法,在此记录一下。
先通过深度优先搜索其中一个岛屿,并且将数字变为2,与另一座岛屿区分;然后再通过广度优先搜索,一层一层架桥,直到遇到另一座岛。
class Solution {
vector<int>direction{
-1,0,1,0,-1};
public:
int shortestBridge(vector<vector<int>>& grid) {
int m=grid.size(),n=grid[0].size();
queue<pair<int,int>>points;
//dfs寻找第一个岛屿,并把1全部赋值为2
bool flipped=false;
for(int i=0;i<m;++i){
if(flipped) break;
for(int j=0;j<n;++j){
if(grid[i][j]==1){
dfs(points,grid,m,n,i,j);
flipped=true;
break;
}
}
}
//bfs寻找第二个岛屿,并把过程中经过的0赋值为2
int x,y;
int level=0;
while(!points.empty()){
//处理第一层0
++level;
int n_points=points.size();
while(n_points--){
//遍历第一层所有的点
auto [r,c]=points.front();
points.pop();
for(int k=0;k<4;++k){
x=r+direction[k],y=c+direction[k+1];
if(x>=0&&x<m&&y>=0&&y<n){
if(grid[x][y]==2){
//假如是2,啥也不做
continue;
}
if(grid[x][y]==1){
//假如遇到了另一座岛了,return level
return level;
}
if(grid[x][y]==0){
//假如遇到的还是0,那么就是下一层的0
points.push({
x,y});
//这个0变为2
grid[x][y]=2;
}
}
}
}
}
return 0;
}
//辅函数
void dfs(queue<pair<int,int>> &points,vector<vector<int>> &grid,
int m,int n,int i,int j)
{
if(i<0||j<0||i==m||j==n||grid[i][j]==2){
return;
}
if(grid[i][j]==0){
points.push({
i,j});
return;
}
grid[i][j]=2;
dfs(points,grid,m,n,i-1,j);
dfs(points,grid,m,n,i+1,j);
dfs(points,grid,m,n,i,j-1);
dfs(points,grid,m,n,i,j+1);
}
};
边栏推荐
- Basic syntax of MySQL DDL and DML and DQL
- MySQL fuzzy query performance optimization
- 每日练习------输出一个整数的二进制数、八进制数、十六进制数。
- 排列数字(DAY90)dfs
- Programmers make money and practice, teach you how to do paid courses, self-media, paid articles and paid technical courses to make money
- Prime numbers (Tsinghua University computer test questions) (DAY 86)
- [Image processing] Image skeleton extraction based on central axis transformation with matlab code
- ezTrack-master使用教程
- It is enough for MySQL to have this article (37k words, just like Bojun!!!)
- 微信小程序开发学习
猜你喜欢

使用DataEase开源工具制作一个高质量的数据大屏
![[Image processing] Image skeleton extraction based on central axis transformation with matlab code](/img/56/cfef9389291fa39612d55a07e1dbb3.png)
[Image processing] Image skeleton extraction based on central axis transformation with matlab code

2022年SQL大厂高频实战面试题(详细解析)

cmd (command line) to operate or connect to the mysql database, and to create databases and tables

安装pytorch

Learn FPGA from the underlying structure (6) ---- Distributed RAM (DRAM, Distributed RAM)

PyCharm使用教程(较详细,图+文)

【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码

cnpm installation steps

2022年比若依更香的开源项目
随机推荐
Introduction to Oracle Patch System and Opatch Tool
create-nuxt-app创建出来的项目没有server
"Hou Lang" programmer version, a speech dedicated to a new generation of programmers, He Bing's "Hou Lang" speech imitation show
4、nerf(pytorch)
Navicat new database
【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码
Solve the problem that the local nacos is not configured but the localhost8848 connection exception always occurs
cross_val_score的用法
图形镜像对称(示意图)
MySQL database basics (a systematic introduction)
What is SOA (Service Oriented Architecture)?
[Other] DS5
MySQL 数据库基础知识(系统化一篇入门)
MySQL fuzzy query performance optimization
It's time to have to learn English, give yourself multiple paths
MySQL(3)
号称年薪30万占比最多的专业,你知道是啥嘛?
Mysql8.+学习笔记
navicat无法连接mysql超详细处理方法
子查询作为检索表时的不同使用场景以及是否需要添加别名的问题