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


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

    目录

    题目一: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. };

  • 相关阅读:
    理解http中cookie!C/C++实现网络的HTTP cookie
    设计模式:策略模式(C#、JAVA、JavaScript、C++、Python、Go、PHP)
    初识js作用域链
    零基础入行IC,选模拟版图还是数字后端?
    Promethus+node_exporter集群部署监控
    Python 并行计算
    Vue高级--前后端分离
    MapReduce编程:join操作和聚合操作
    香港优才计划需要什么条件?2023申请条件/流程/政策放宽!
    Skip Index 学习
  • 原文地址:https://blog.csdn.net/qq_53763705/article/details/127064880