• 十四天算法快速入门第九天之「广度优先搜索 / 深度优先搜索」


    写在前面:本文题单均来自力扣的算法刷题计划,开这个系列主要是以题目与题解的形式对一些常见、常用算法进行归类和总结,希望在促进自己学习的同时也能帮助到看到这篇文章的大家。另外,本文并非一天一更~

    目录

    题目一:542. 01 矩阵 

    题目描述:

    题目分析:

    题解代码:

    题目二:994. 腐烂的橘子

    题目描述:

    题目分析:

    题解代码:


    题目一:542. 01 矩阵 

    题目描述:

    题目分析:

    这题运用BFS可以求解,我们可以先创建一个ret数组记录mat数组里每个数到0的最短距离,将mat数组里为0的数都在其内记录为0为1的数暂且将其到0的距离记录为一个较大的数(不小于m*n),接着创建一个队列que对mat中的每一个0依次入队,再逐一将其上下左右拓展,若遇到1时,也就是当前位置在ret内记录的距离大于上一步的位置在ret内记录的距离时,取上一步的位置在ret内记录的距离再+1,保证为最短距离,每一个位置拓展完后再出队即可。

    题解代码:

    1. class Solution {
    2. public:
    3. vectorint>> updateMatrix(vectorint>>& mat)
    4. {
    5. int r=mat.size(); //mat数组的长、宽
    6. int c=mat[0].size();
    7. vectorint,int>> step={{0,-1},{0,1},{-1,0},{1,0}};
    8. vectorint>> ret(r,vector<int>(c,INT_MAX)); //初始化ret数组
    9. queueint,int>> que;
    10. for(int i=0;i
    11. {
    12. for(int j=0;j
    13. {
    14. if(mat[i][j]==0)
    15. {
    16. ret[i][j]=0; //记录0到0的最短距离就是0
    17. que.push({i,j}); //将所有0入队
    18. }
    19. }
    20. }
    21. while(!que.empty())
    22. {
    23. auto temp=que.front(); //auto自动识别数据类型
    24. que.pop();
    25. for(int i=0;i<4;i++) //进行拓展
    26. {
    27. int x=temp.first+step[i].first;
    28. int y=temp.second+step[i].second;
    29. if(x=0 && y>=0) //防止越界
    30. {
    31. if(ret[x][y]>ret[temp.first][temp.second]+1) //遇到1了
    32. {
    33. ret[x][y]=ret[temp.first][temp.second]+1;
    34. que.push({x,y}); //出队
    35. }
    36. }
    37. }
    38. }
    39. return ret;
    40. }
    41. };

    题目二:994. 腐烂的橘子

    题目描述:

     

    题目分析:

    经典BFS问题,先将所有腐烂的橘子入队,再同时向上下左右扩散,遇到新鲜的橘子使其腐烂,以此类推,最后再判断有无新鲜的橘子即可。  

    题解代码:

    1. class Solution {
    2. public:
    3. int orangesRotting(vectorint>>& grid)
    4. {
    5. int r = grid.size();
    6. int c = grid[0].size();
    7. vectorint, int>> step = { {-1,0},{1,0},{0,1},{0,-1} };
    8. queueint, int>> que;
    9. for (int i = 0; i < r; i++) //初始化队列
    10. {
    11. for (int j = 0; j < c; j++)
    12. {
    13. if (grid[i][j] == 2)
    14. {
    15. que.push({ i,j });
    16. }
    17. }
    18. }
    19. int time = 0;
    20. while (!que.empty())
    21. {
    22. int size=que.size();
    23. while(size--) //当前一波腐烂的橘子同时进行扩散
    24. {
    25. auto temp = que.front();
    26. que.pop();
    27. for (int i = 0; i < 4; i++) //开始扩散
    28. {
    29. int x = temp.first + step[i].first;
    30. int y = temp.second + step[i].second;
    31. if (x >= 0 && x < r && y >= 0 && y < c)
    32. {
    33. if (grid[x][y] == 1)
    34. {
    35. grid[x][y] = 2;
    36. que.push({ x,y });
    37. }
    38. }
    39. }
    40. }
    41. if (!que.empty()) //扩散一轮后计时
    42. time++;
    43. }
    44. for (int i = 0; i < r; i++) //最后再判断还有无新鲜橘子
    45. {
    46. for (int j = 0; j < c; j++)
    47. {
    48. if (grid[i][j] == 1)
    49. return -1;
    50. }
    51. }
    52. return time;
    53. }
    54. };

  • 相关阅读:
    macOS Ventura13.0.1解决office缺少“宋体”等问题。安装微软雅黑、宋体等字体。
    java基于SpringBoot+Vue+nodejs 的社区团购系统 elementui
    第8天:可变与不可变类型、字典、元组与集合的内置方法
    Linux 主进程管理
    10.26~10.27论文,ALP: AdaptiveLossless floating-Point Compression
    领先一步,效率翻倍:PieCloudDB Database 预聚集特性让查询速度飞起来!
    淘宝详情源数据接口解析
    【C++入门篇】深入理解函数重载
    变量、流程控制与游标(MySQL)
    【STM32 CubeMX】移植u8g2(一次成功)
  • 原文地址:https://blog.csdn.net/qq_53763705/article/details/127064880