

超级源点:其实就是把相应的原点一次性都丢到队列中


- class Solution {
- public:
- const int dx[4]={1,-1,0,0};
- const int dy[4]={0,0,1,-1};
- vector
int>> updateMatrix(vectorint>>& mat) { - //多源BFS 正难则反,以0为起点向外扩展
- int m=mat.size(),n=mat[0].size();
- vector
int>> dis(m,vector<int>(n,-1));//要输出的数组 -1表示没有搜索过 - queue
int,int>> q;//存储起点 - for(int i=0;i
- for(int j=0;j
- if(mat[i][j]==0)
- {
- q.emplace(i,j);
- dis[i][j]=0;
- }
- //不需要标记数组 不需要step 也不需要控制一层一层出sz
- //因为dis数组不仅可以标记哪些地方没有搜索过或者搜索过,而且存储了最短距离
- while(!q.empty())
- {
- auto[a,b]=q.front();
- q.pop();
- for(int k=0;k<4;++k)
- {
- int x=dx[k]+a,y=dy[k]+b;
- if(x>=0&&x
=0&&y-1) - {
- dis[x][y]=dis[a][b]+1;
- q.emplace(x,y);
- }
- }
- }
- return dis;
- }
- };
三、飞地的数量


- class Solution {
- public:
- //正难则反
- const int dx[4]={1,-1,0,0};
- const int dy[4]={0,0,1,-1};
- int numEnclaves(vector
int >>& grid) { - int m=grid.size(),n=grid[0].size();
- //从边开始进行一次宽搜 将可以走出边界的标记一下
- vector
bool>> vis(m,vector<bool>(n)); - //将边界1的都丢到队列中
- queue
int,int>> q; - for(int i=0;i
//第一行和最后一行 - for(int j=0;j
- if(i==0||i==m-1||j==0||j==n-1)
- if(grid[i][j]==1)
- {
- q.emplace(i,j);
- vis[i][j]=true;
- }
- //进行多源BFS
- while(!q.empty())
- {
- auto [a,b]=q.front();
- q.pop();
- for(int k=0;k<4;++k)
- {
- int x=dx[k]+a,y=dy[k]+b;
- if(x>=0&&x
=0&&y1&&vis[x][y]==false) - {
- q.emplace(x,y);
- vis[x][y]=true;
- }
- }
- }
- //处理完之后,遍历一下找到没有被标记且为1的单元格 就可以统计个数了
- int ret=0;
- for(int i=0;i
- for(int j=0;j
- if(grid[i][j]==1&&vis[i][j]==false)
- ++ret;
- return ret;
- }
- };
四、地球中的最高点


- class Solution {
- public:
- const int dx[4]={1,-1,0,0};
- const int dy[4]={0,0,1,-1};
- vector
int>> highestPeak(vectorint>>& isWater) { - int m=isWater.size(),n=isWater[0].size();
- vector
int>> vv(m,vector<int>(n,-1)); - //正难则反
- queue
int,int>> q; - for(int i=0;i
- for(int j=0;j
- if(isWater[i][j]==1)
- {
- q.emplace(i,j);
- vv[i][j]=0;
- }
- //多源BFS
- while(!q.empty())
- {
- auto[a,b]=q.front();
- q.pop();
- for(int k=0;k<4;++k)
- {
- int x=dx[k]+a,y=dy[k]+b;
- if(x>=0&&x
=0&&y-1) - {
- vv[x][y]=vv[a][b]+1;
- q.emplace(x,y);
- }
- }
- }
- return vv;
- }
- };
五、地图分析


- class Solution {
- public:
- const int dx[4]={1,-1,0,0};
- const int dy[4]={0,0,1,-1};
- int maxDistance(vector
int >>& grid) - {
- int m=grid.size(),n=grid[0].size();
- vector
int>> vv(m,vector<int>(n,-1)); - queue
int,int>> q; - for(int i=0;i
- for(int j=0;j
- if(grid[i][j]==1)
- {
- q.emplace(i,j);
- vv[i][j]=0;
- }
- //多源BFS
- int ret=-1;//如果只有海洋或者只有陆地,那么就会直接返回-1
- while(!q.empty())
- {
- auto[a,b]=q.front();
- q.pop();
- for(int k=0;k<4;++k)
- {
- int x=dx[k]+a,y=dy[k]+b;
- if(x>=0&&x
=0&&y-1) - {
- vv[x][y]=vv[a][b]+1;
- q.emplace(x,y);
- ret=max(ret,vv[x][y]);
- }
- }
- }
- return ret;
- }
- };

-
相关阅读:
firewalld防火墙基础
CSS 三栏布局
PAM从入门到精通(二十三)
计算机——数据库
SpringCloud
【3D游戏建模全流程教学】在 ZBrush、Maya 和 Arnold 中制作雪矮人
Visual Studio 2022 cmake编译 PP-OCRv4
机器学习课后习题 --回归
[算法刷题笔记]二叉树练习(2):对称二叉树有关的练习
(Carousel)解决:Element-ui 中 Carousel 走马灯的样式的修改问题
-
原文地址:https://blog.csdn.net/weixin_51142926/article/details/139568615