• BFS:多源BFS问题


    一、多源BFS简介

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

    二、01矩阵

    . - 力扣(LeetCode)

    1. class Solution {
    2. public:
    3. const int dx[4]={1,-1,0,0};
    4. const int dy[4]={0,0,1,-1};
    5. vectorint>> updateMatrix(vectorint>>& mat) {
    6. //多源BFS 正难则反,以0为起点向外扩展
    7. int m=mat.size(),n=mat[0].size();
    8. vectorint>> dis(m,vector<int>(n,-1));//要输出的数组 -1表示没有搜索过
    9. queueint,int>> q;//存储起点
    10. for(int i=0;i
    11. for(int j=0;j
    12. if(mat[i][j]==0)
    13. {
    14. q.emplace(i,j);
    15. dis[i][j]=0;
    16. }
    17. //不需要标记数组 不需要step 也不需要控制一层一层出sz
    18. //因为dis数组不仅可以标记哪些地方没有搜索过或者搜索过,而且存储了最短距离
    19. while(!q.empty())
    20. {
    21. auto[a,b]=q.front();
    22. q.pop();
    23. for(int k=0;k<4;++k)
    24. {
    25. int x=dx[k]+a,y=dy[k]+b;
    26. if(x>=0&&x=0&&y-1)
    27. {
    28. dis[x][y]=dis[a][b]+1;
    29. q.emplace(x,y);
    30. }
    31. }
    32. }
    33. return dis;
    34. }
    35. };

    三、飞地的数量

    . - 力扣(LeetCode)

    1. class Solution {
    2. public:
    3. //正难则反
    4. const int dx[4]={1,-1,0,0};
    5. const int dy[4]={0,0,1,-1};
    6. int numEnclaves(vectorint>>& grid) {
    7. int m=grid.size(),n=grid[0].size();
    8. //从边开始进行一次宽搜 将可以走出边界的标记一下
    9. vectorbool>> vis(m,vector<bool>(n));
    10. //将边界1的都丢到队列中
    11. queueint,int>> q;
    12. for(int i=0;i//第一行和最后一行
    13. for(int j=0;j
    14. if(i==0||i==m-1||j==0||j==n-1)
    15. if(grid[i][j]==1)
    16. {
    17. q.emplace(i,j);
    18. vis[i][j]=true;
    19. }
    20. //进行多源BFS
    21. while(!q.empty())
    22. {
    23. auto [a,b]=q.front();
    24. q.pop();
    25. for(int k=0;k<4;++k)
    26. {
    27. int x=dx[k]+a,y=dy[k]+b;
    28. if(x>=0&&x=0&&y1&&vis[x][y]==false)
    29. {
    30. q.emplace(x,y);
    31. vis[x][y]=true;
    32. }
    33. }
    34. }
    35. //处理完之后,遍历一下找到没有被标记且为1的单元格 就可以统计个数了
    36. int ret=0;
    37. for(int i=0;i
    38. for(int j=0;j
    39. if(grid[i][j]==1&&vis[i][j]==false)
    40. ++ret;
    41. return ret;
    42. }
    43. };

    四、地球中的最高点

    . - 力扣(LeetCode)

    1. class Solution {
    2. public:
    3. const int dx[4]={1,-1,0,0};
    4. const int dy[4]={0,0,1,-1};
    5. vectorint>> highestPeak(vectorint>>& isWater) {
    6. int m=isWater.size(),n=isWater[0].size();
    7. vectorint>> vv(m,vector<int>(n,-1));
    8. //正难则反
    9. queueint,int>> q;
    10. for(int i=0;i
    11. for(int j=0;j
    12. if(isWater[i][j]==1)
    13. {
    14. q.emplace(i,j);
    15. vv[i][j]=0;
    16. }
    17. //多源BFS
    18. while(!q.empty())
    19. {
    20. auto[a,b]=q.front();
    21. q.pop();
    22. for(int k=0;k<4;++k)
    23. {
    24. int x=dx[k]+a,y=dy[k]+b;
    25. if(x>=0&&x=0&&y-1)
    26. {
    27. vv[x][y]=vv[a][b]+1;
    28. q.emplace(x,y);
    29. }
    30. }
    31. }
    32. return vv;
    33. }
    34. };

     五、地图分析

    . - 力扣(LeetCode)

    1. class Solution {
    2. public:
    3. const int dx[4]={1,-1,0,0};
    4. const int dy[4]={0,0,1,-1};
    5. int maxDistance(vectorint>>& grid)
    6. {
    7. int m=grid.size(),n=grid[0].size();
    8. vectorint>> vv(m,vector<int>(n,-1));
    9. queueint,int>> q;
    10. for(int i=0;i
    11. for(int j=0;j
    12. if(grid[i][j]==1)
    13. {
    14. q.emplace(i,j);
    15. vv[i][j]=0;
    16. }
    17. //多源BFS
    18. int ret=-1;//如果只有海洋或者只有陆地,那么就会直接返回-1
    19. while(!q.empty())
    20. {
    21. auto[a,b]=q.front();
    22. q.pop();
    23. for(int k=0;k<4;++k)
    24. {
    25. int x=dx[k]+a,y=dy[k]+b;
    26. if(x>=0&&x=0&&y-1)
    27. {
    28. vv[x][y]=vv[a][b]+1;
    29. q.emplace(x,y);
    30. ret=max(ret,vv[x][y]);
    31. }
    32. }
    33. }
    34. return ret;
    35. }
    36. };

  • 相关阅读:
    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