IT评测·应用市场-qidao123.com技术社区

标题: 专题十四——BFS [打印本页]

作者: 用户云卷云舒    时间: 2025-1-2 15:08
标题: 专题十四——BFS
目录
一BFS办理FloodFill算法
1图像渲染
2岛屿数量
3岛屿的最大面积 
4被围绕的区域
二BFS办理蛋源最短路径题目
1迷宫中离入口最近的出口
2最小基因厘革
3单词接龙 
4为高尔夫比赛砍树
三BFS办理多源最短路径题目 
1 01矩阵
2飞地的数量
 3舆图中的最高点
4舆图分析

一BFS办理FloodFill算法

floodfill算法中文名:洪水灌溉:这类题目都是要来找出性子相同的联通块;办理好一道后,反面遇到类型的题目代码也是类似的!
1图像渲染

oj链接:图像渲染
   解法:bfs
  1.先把开始的坐标{sr,sc}放进队列中;
  2.取出队头坐标{x,y},直接将它改成color(队列坐标都是合法的,待改的),再把它上下左右的坐标(等于image[sr][sc])放到队列中...
  1. //dfs
  2. class Solution
  3. {
  4. public:
  5.     int target, newcolor, m, n;
  6.     int dx[4] = {1, -1, 0, 0};
  7.     int dy[4] = {0, 0, 1, -1};
  8.     void dfs(vector<vector<int>> &image, int i, int j)
  9.     {
  10.         image[i][j] = newcolor;
  11.         for (int a = 0; a < 4; a++)
  12.         {
  13.             int x = i + dx[a], y = j + dy[a];
  14.             if (x >= 0 && x < m && y >= 0 && y < n && image[x][y] == target)
  15.             {
  16.                 dfs(image, x, y);
  17.             }
  18.         }
  19.     }
  20.     vector<vector<int>> floodFill(vector<vector<int>> &image, int sr, int sc, int color)
  21.     {
  22.         if (image[sr][sc] == color)
  23.             return image; // 特殊判断
  24.         m = image.size(), n = image[0].size();
  25.         target = image[sr][sc];
  26.         newcolor = color;
  27.         dfs(image, sr, sc);
  28.         return image;
  29.     }
  30. };
  31. //bfs
  32. class Solution
  33. {
  34. public:
  35.     vector<vector<int>> floodFill(vector<vector<int>> &image, int sr, int sc, int color)
  36.     {
  37.         if (image[sr][sc] == color)
  38.             return image; // 特殊判断
  39.         // bfs前准备
  40.         int dx[4] = {1, -1, 0, 0};
  41.         int dy[4] = {0, 0, 1, -1};
  42.         int m = image.size(), n = image[0].size();
  43.         int target = image[sr][sc];
  44.         // bfs主逻辑
  45.         queue<pair<int, int>> qe;
  46.         qe.push({sr, sc});
  47.         while (qe.size())
  48.         {
  49.             int sz = qe.size();
  50.             for (int i = 0; i < sz; i++)
  51.             {
  52.                 auto &[a, b] = qe.front();
  53.                 qe.pop();
  54.                 if (image[a][b] == target)
  55.                     image[a][b] = color;
  56.                 for (int j = 0; j < 4; j++)
  57.                 {
  58.                     int x = a + dx[j], y = b + dy[j];
  59.                     if (x >= 0 && x < m && y >= 0 && y < n && image[x][y] == target)
  60.                     {
  61.                         qe.push({x, y});
  62.                     }
  63.                 }
  64.             }
  65.         }
  66.         return image;
  67.     }
  68. };
复制代码
2岛屿数量

oj链接:岛屿数量
   解法:bfs
  1. 两层for循环找‘1’&&该位置没被标志,将该坐标进队列,执行一次bfs;
  2.bfs的主逻辑与上题类似:但在这里要注意:坐标进队列后要立刻马上举行标志!如果是取出节点后标志的话,会有重复节点进队列导致超时!!
  1. //dfs
  2. class Solution
  3. {
  4. public:
  5.     bool vis[300][300] = {false};
  6.     int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
  7.     int m, n;
  8.     void dfs(vector<vector<char>> &grid, int i, int j)
  9.     {
  10.         vis[i][j] = true;
  11.         for (int a = 0; a < 4; a++)
  12.         {
  13.             int x = i + dx[a], y = j + dy[a];
  14.             if (x >= 0 && x < m && y >= 0 && y < n && !vis[x][y] && grid[x][y] == '1')
  15.             {
  16.                 dfs(grid, x, y);
  17.             }
  18.         }
  19.     }
  20.     int numIslands(vector<vector<char>> &grid)
  21.     {
  22.         m = grid.size(), n = grid[0].size();
  23.         int ret = 0;
  24.         for (int i = 0; i < m; i++)
  25.         {
  26.             for (int j = 0; j < n; j++)
  27.             {
  28.                 if (grid[i][j] == '1' && !vis[i][j])
  29.                 {
  30.                     ret++;
  31.                     dfs(grid, i, j);
  32.                 }
  33.             }
  34.         }
  35.         return ret;
  36.     }
  37. };
  38. //bfs
  39. class Solution
  40. {
  41. public:
  42.     bool vis[300][300] = {false};
  43.     int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
  44.     int m, n;
  45.     queue<pair<int, int>> q;
  46.     void bfs(vector<vector<char>> &grid, int i, int j)
  47.     {
  48.         //进队列后必须立刻进行标记!
  49.         q.push({i, j});
  50.         vis[i][j] = true;
  51.         while (q.size())
  52.         {
  53.             auto [a, b] = q.front();
  54.             q.pop();
  55.             // vis[a][b]=true;节点会重复进队列,导致超时
  56.             for (int k = 0; k < 4; k++)
  57.             {
  58.                 int x = a + dx[k], y = b + dy[k];
  59.                 if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == '1' && !vis[x][y])
  60.                 {
  61.                     q.push({x, y});
  62.                     vis[x][y] = true;
  63.                 }
  64.             }
  65.         }
  66.     }
  67.     int numIslands(vector<vector<char>> &grid)
  68.     {
  69.         m = grid.size(), n = grid[0].size();
  70.         int ret = 0;
  71.         for (int i = 0; i < m; i++)
  72.         {
  73.             for (int j = 0; j < n; j++)
  74.             {
  75.                 if (grid[i][j] == '1' && !vis[i][j])
  76.                 {
  77.                     ret++;
  78.                     bfs(grid, i, j);
  79.                 }
  80.             }
  81.         }
  82.         return ret;
  83.     }
  84. };
复制代码
3岛屿的最大面积 

oj链接:岛屿的最大面积
   与上题思路类似:只不过我们要求的是最大面积,我们可以:举行dfs后把岛屿的面积带出来
  1. class Solution
  2. {
  3. public:
  4.     int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, 1, -1};
  5.     bool vis[50][50] = {false};
  6.     int m, n;
  7.     queue<pair<int, int>> q;
  8.     void bfs(vector<vector<int>> &grid, int i, int j, int *area)
  9.     {
  10.         q.push({i, j});
  11.         vis[i][j] = true;
  12.         while (q.size())
  13.         {
  14.             auto [a, b] = q.front();
  15.             q.pop();
  16.             for (int k = 0; k < 4; k++)
  17.             {
  18.                 int x = a + dx[k], y = b + dy[k];
  19.                 if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 && !vis[x][y])
  20.                 {
  21.                     q.push({x, y});
  22.                     vis[x][y] = true;
  23.                     (*area)++;
  24.                 }
  25.             }
  26.         }
  27.     }
  28.     int maxAreaOfIsland(vector<vector<int>> &grid)
  29.     {
  30.         m = grid.size(), n = grid[0].size();
  31.         int MaxArea = 0;
  32.         for (int i = 0; i < m; i++)
  33.         {
  34.             for (int j = 0; j < n; j++)
  35.             {
  36.                 if (grid[i][j] == 1 && !vis[i][j])
  37.                 {
  38.                     int area = 1; // 从该位置进行bfs:找出该位置处岛屿的面积
  39.                     bfs(grid, i, j, &area);
  40.                     MaxArea = max(MaxArea, area);
  41.                 }
  42.             }
  43.         }
  44.         return MaxArea;
  45.     }
  46. };
复制代码
4被围绕的区域

oj链接:被围绕的区域
   解法:bfs
  此题在递归 搜刮与回溯算法题 
   
  1. class Solution
  2. {
  3. public:
  4.     bool vis[200][200];
  5.     int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
  6.     int m, n;
  7.     queue<pair<int, int>> q;
  8.     void bfs(vector<vector<char>> &board, int i, int j)
  9.     {
  10.         q.push({i, j});
  11.         vis[i][j] = true;
  12.         while (q.size())
  13.         {
  14.             auto [a, b] = q.front();
  15.             q.pop();
  16.             for (int k = 0; k < 4; k++)
  17.             {
  18.                 int x = a + dx[k], y = b + dy[k];
  19.                 if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'O' && !vis[x][y])
  20.                 {
  21.                     q.push({x, y});
  22.                     vis[x][y] = true;
  23.                 }
  24.             }
  25.         }
  26.     }
  27.     void solve(vector<vector<char>> &board)
  28.     {
  29.         m = board.size(), n = board[0].size();
  30.         for (int i = 0; i < m; i++)
  31.         {
  32.             if (board[i][0] == 'O')
  33.                 bfs(board, i, 0);
  34.             if (board[i][n - 1] == 'O')
  35.                 bfs(board, i, n - 1);
  36.         }
  37.         for (int j = 0; j < n; j++)
  38.         {
  39.             if (board[0][j] == 'O')
  40.                 bfs(board, 0, j);
  41.             if (board[m - 1][j] == 'O')
  42.                 bfs(board, m - 1, j);
  43.         }
  44.         for (int i = 0; i < m; i++)
  45.         {
  46.             for (int j = 0; j < n; j++)
  47.             {
  48.                 if (board[i][j] == 'O' && !vis[i][j])
  49.                     board[i][j] = 'X';
  50.             }
  51.         }
  52.     }
  53. };
复制代码
二BFS办理蛋源最短路径题目


1迷宫中离入口最近的出口

oj链接:迷宫中离入口最近的出口
   从开始粗来一次bfs遍历即可;如果进队列的坐标是出口,直接返回遍历的层数ret即可!
  1. class Solution
  2. {
  3. public:
  4.     bool vis[100][100];
  5.     int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
  6.     int m, n;
  7.     int nearestExit(vector<vector<char>> &maze, vector<int> &entrance)
  8.     {
  9.         m = maze.size(), n = maze[0].size();
  10.         queue<pair<int, int>> q;
  11.         int beginx = entrance[0], beginy = entrance[1];
  12.         q.push({beginx, beginy});
  13.         vis[beginx][beginy] = true;
  14.         int ret = 0;
  15.         while (q.size())
  16.         {
  17.             ret++; // 往外扩
  18.             int sz = q.size();
  19.             for (int i = 0; i < sz; i++)
  20.             {
  21.                 auto [a, b] = q.front();
  22.                 q.pop();
  23.                 for (int k = 0; k < 4; k++)
  24.                 {
  25.                     int x = a + dx[k], y = b + dy[k];
  26.                     if (x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == '.' &&!vis[x][y])
  27.                     {
  28.                         if (x == 0 || x == m - 1 || y == 0 || y == n - 1)
  29.                             return ret; // 找到出口了,直接返回遍历的层数,即最短路径
  30.                         q.push({x, y});
  31.                         vis[x][y] = true;
  32.                     }
  33.                 }
  34.             }
  35.         }
  36.         return -1;
  37.     }
  38. };
复制代码
2最小基因厘革

oj链接:最小基因厘革

  1. class Solution
  2. {
  3. public:
  4.     int minMutation(string startGene, string endGene, vector<string> &bank)
  5.     {
  6.         char arr[4] = {'A', 'G', 'C', 'T'};
  7.         unordered_map<string, bool> vis;
  8.         for (auto &s : bank)
  9.             vis[s] = false;
  10.         if(!vis.count(endGene)) return -1;// 不存在直接返回
  11.         queue<string> q;
  12.         q.push(startGene);
  13.         vis[startGene] = true; // 有可能基因库也有这个,提前标记
  14.         int ret = 0;
  15.         while (q.size())
  16.         {
  17.             ret++;
  18.             int sz = q.size();
  19.             for (int i = 0; i < sz; i++)
  20.             {
  21.                 string s = q.front();
  22.                 q.pop();
  23.                 for (int i = 0; i < 8; i++)
  24.                 {
  25.                     for (int j = 0; j < 4; j++)
  26.                     {
  27.                         string tmp = s;
  28.                         tmp[i] = arr[j]; // 这里就没必要判断tmp[i]是否与arr[j]相等:因为前面我们已经标记过了
  29.                         if (vis.count(tmp) && !vis[tmp])
  30.                         {
  31.                             if (tmp == endGene)
  32.                                 return ret;
  33.                             q.push(tmp);
  34.                             vis[tmp] = true;
  35.                         }
  36.                     }
  37.                 }
  38.             }
  39.         }
  40.         return -1;
  41.     }
  42. };
复制代码
3单词接龙 

oj链接:单词接龙
   与上题思路类似 
  1. class Solution
  2. {
  3. public:
  4.     int ladderLength(string beginWord, string endWord, vector<string> &wordList)
  5.     {
  6.         string t = "qwertyuiopasdfghjklzxcvbnm";
  7.         unordered_map<string, bool> vis;
  8.         for (auto &word : wordList)
  9.             vis[word] = false;
  10.         if (!vis.count(endWord))
  11.             return 0;
  12.         int ans = 1;
  13.         queue<string> qe;
  14.         qe.push(beginWord);
  15.         vis[beginWord] = true;
  16.         while (qe.size())
  17.         {
  18.             ans++;
  19.             int sz = qe.size();
  20.             while (sz--)
  21.             {
  22.                 string s = qe.front();
  23.                 qe.pop();
  24.                 for (int i = 0; i < s.size(); i++)
  25.                 {
  26.                     char ch = s[i];
  27.                     for (int j = 0; j < 26; j++)
  28.                     {
  29.                         s[i] = t[j];
  30.                         if (vis.count(s) && !vis[s])
  31.                         {
  32.                             if (s == endWord)
  33.                                 return ans;
  34.                             qe.push(s);
  35.                             vis[s] = true;
  36.                         }
  37.                     }
  38.                     s[i] = ch;
  39.                 }
  40.             }
  41.         }
  42.         return 0;
  43.     }
  44. };
复制代码
4为高尔夫比赛砍树

oj链接:为高尔夫比赛砍树
   注意:
 
  不是说肯定要砍,当初做的时候就是看劈叉了~
  

  1. class Solution
  2. {
  3. public:
  4.     typedef pair<int, int> PII;
  5.     int n, m;
  6.     int cutOffTree(vector<vector<int>> &forest)
  7.     {
  8.         // 先预处理砍树顺序
  9.         vector<PII> trees;
  10.         n = forest.size(), m = forest[0].size();
  11.         for (int i = 0; i < n; i++)
  12.         {
  13.             for (int j = 0; j < m; j++)
  14.             {
  15.                 if (forest[i][j] > 1)
  16.                     trees.push_back({i, j});
  17.             }
  18.         }
  19.         sort(trees.begin(), trees.end(), [&](const PII &p1, const PII &p2)
  20.              { return forest[p1.first][p1.second] < forest[p2.first][p2.second]; });
  21.         // 若干个迷宫问题的最小步数累加
  22.         int dx = 0, dy = 0;
  23.         int ret = 0;
  24.         for (auto &[a, b] : trees)
  25.         {
  26.             int step = bfs(forest, dx, dy, a, b);
  27.             if (step == -1)
  28.                 return -1;
  29.             ret += step;
  30.             dx = a, dy = b; // 更新位置
  31.         }
  32.         return ret;
  33.     }
  34.     int dx[4] = {0, 0, 1, -1};
  35.     int dy[4] = {1, -1, 0, 0};
  36.     int bfs(vector<vector<int>> &forest, int bx, int by, int ex, int ey)
  37.     {
  38.         if (bx == ex && by == ey)
  39.             return 0;
  40.         int step = 0;
  41.         queue<PII> qe;
  42.         bool vis[50][50] = {false};
  43.         // 层序遍历主逻辑
  44.         qe.push({bx, by});
  45.         vis[bx][by] = true;
  46.         while (qe.size())
  47.         {
  48.             step++;
  49.             int size = qe.size();
  50.             while (size--)
  51.             {
  52.                 auto [a, b] = qe.front();
  53.                 qe.pop();
  54.                 for (int i = 0; i < 4; i++)
  55.                 {
  56.                     int x = dx[i] + a, y = dy[i] + b;
  57.                     if (x >= 0 && x < n && y >= 0 && y < m && !vis[x][y] && forest[x][y] != 0)
  58.                     {
  59.                         if (x == ex && y == ey)
  60.                             return step;
  61.                         vis[x][y] = true;
  62.                         qe.push({x, y});
  63.                     }
  64.                 }
  65.             }
  66.         }
  67.         return -1;
  68.     }
  69. };
复制代码
三BFS办理多源最短路径题目 


1 01矩阵

oj链接:01 矩阵
   解法:BFS + 正难则反
  如果从1作为起点走到尽头0,那比及尽头时记载距离时就不知道是从哪一个1为起点的
  以是我们从0为起点开始,走到不是0的位置就举行距离记载,同时把它
  

  1. class Solution
  2. {
  3. public:
  4.     // bool vis[1000][1000];//开10000会溢出?
  5.     int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
  6.     int m, n;
  7.     vector<vector<int>> updateMatrix(vector<vector<int>> &mat)
  8.     {
  9.         m = mat.size(), n = mat[0].size();
  10.         vector<vector<int>> dist(m, vector<int>(n, -1));
  11.         queue<pair<int, int>> q;
  12.         for (int i = 0; i < m; i++)
  13.         {
  14.             for (int j = 0; j < n; j++)
  15.             {
  16.                 if (mat[i][j] == 0)
  17.                 {
  18.                     dist[i][j] = 0;
  19.                     q.push({i, j});
  20.                 }
  21.             }
  22.         }
  23.         // int step=0;
  24.         while (q.size())
  25.         {
  26.             // step++;
  27.             // int sz=q.size();
  28.             // while(sz--)
  29.             // {
  30.             auto [a, b] = q.front();
  31.             q.pop();
  32.             for (int i = 0; i < 4; i++)
  33.             {
  34.                 int x = dx[i] + a, y = dy[i] + b;
  35.                 // if(x>=0&&x<m&&y>=0&&y<n&&!vis[x][y])
  36.                 if (x >= 0 && x < m && y >= 0 && y < n && dist[x][y] == -1)
  37.                 {
  38.                     // dist[x][y]=cnt;
  39.                     // vis[x][y]=true;
  40.                     dist[x][y] = dist[a][b] + 1;
  41.                     q.push({x, y});
  42.                 }
  43.             }
  44.             // }
  45.         }
  46.         return dist;
  47.     }
  48. };
复制代码
2飞地的数量

oj链接:飞地的数量
   解法:dfs + 正难则反
  a先把边界上的1进入队列并举行标志;
  b举行dfs遍历(上下左右找合法坐标)只要它是1而且没被标志,就把它加入到队列中并举行标志;
  c再次对grid举行遍历,只要坐标的值是1而且没别标志就代表它走不出边界,举行统计即可
  1. class Solution
  2. {
  3. public:
  4.     bool vis[500][500];
  5.     int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
  6.     int m, n;
  7.     int numEnclaves(vector<vector<int>> &grid)
  8.     {
  9.         m = grid.size(), n = grid[0].size();
  10.         queue<pair<int, int>> q;
  11.         // 边上1统计
  12.         for (int i = 0; i < m; i++)
  13.         {
  14.             for (int j = 0; j < n; j++)
  15.             {
  16.                 if (i == 0 || i == m - 1 || j == 0 || j == n - 1)
  17.                 {
  18.                     if (grid[i][j] == 1)
  19.                     {
  20.                         q.push({i, j});
  21.                         vis[i][j] = true;
  22.                     }
  23.                 }
  24.             }
  25.         }
  26.         // bfs进行遍历
  27.         while (q.size())
  28.         {
  29.             // int sz=q.size();
  30.             // while(sz--)
  31.             // {
  32.             auto [a, b] = q.front();
  33.             q.pop();
  34.             for (int k = 0; k < 4; k++)
  35.             {
  36.                 int x = dx[k] + a, y = dy[k] + b;
  37.                 if (x >= 0 && x < m && y >= 0 && y < n && !vis[x][y] && grid[x][y] == 1)
  38.                 {
  39.                     q.push({x, y});
  40.                     vis[x][y] = true;
  41.                 }
  42.             }
  43.             // }
  44.         }
  45.         int ret = 0;
  46.         for (int i = 0; i < m; i++)
  47.         {
  48.             for (int j = 0; j < n; j++)
  49.             {
  50.                 if (!vis[i][j] && grid[i][j] == 1)
  51.                     ret++;
  52.             }
  53.         }
  54.         return ret;
  55.     }
  56. };
复制代码
 3舆图中的最高点

oj链接:舆图中的最高点
   解法:bfs
  安排高度时,任意相邻的格子高度差至多为1,也就是既可以计划成0,也可以计划成1;
  但题目要我们返回的是高度值最大的环境,以是我们贪心地把全部高度差都计划成1即可
  也就是从全部水域位置为起点来来依次bfs遍历即可完成使命~
  

  1. class Solution {
  2. public:
  3.     typedef pair<int,int> PII;
  4.     int dx[4]={0,0,1,-1};
  5.     int dy[4]={1,-1,0,0};
  6.     vector<vector<int>> highestPeak(vector<vector<int>>& isWater)
  7.     {
  8.         queue<PII> qe;
  9.         int n=isWater.size(),m=isWater[0].size();
  10.         vector<vector<int>> hight(n,vector<int>(m,-1));
  11.         for(int i=0;i<n;i++)
  12.         {
  13.             for(int j=0;j<m;j++)
  14.             {
  15.                 if(isWater[i][j]==1)
  16.                 {
  17.                     qe.push({i,j});
  18.                     hight[i][j]=0;
  19.                 }
  20.             }
  21.         }
  22.         while(qe.size())
  23.         {
  24.             int sz=qe.size();
  25.             while(sz--)
  26.             {
  27.                 auto[a,b]=qe.front();
  28.                 qe.pop();
  29.                 for(int i=0;i<4;i++)
  30.                 {
  31.                     int x=dx[i]+a,y=dy[i]+b;
  32.                     if(x>=0&&x<n&&y>=0&&y<m&&hight[x][y]==-1)
  33.                     {
  34.                         hight[x][y]=hight[a][b]+1;
  35.                         qe.push({x,y});
  36.                     }
  37.                 }
  38.             }
  39.         }
  40.         return hight;
  41.     }
  42. };
复制代码
4舆图分析

oj链接:舆图分析
    与01矩阵的思路一样~
  1. //用dist数组
  2. class Solution
  3. {
  4. public:
  5.     typedef pair<int, int> PII;
  6.     int dx[4] = {0, 0, 1, -1};
  7.     int dy[4] = {1, -1, 0, 0};
  8.     int maxDistance(vector<vector<int>> &grid)
  9.     {
  10.         int n = grid.size(), m = grid[0].size();
  11.         vector<vector<int>> dist(n, vector<int>(m, -1));
  12.         queue<PII> qe;
  13.         for (int i = 0; i < n; i++)
  14.         {
  15.             for (int j = 0; j < m; j++)
  16.             {
  17.                 if (grid[i][j] == 1)
  18.                 {
  19.                     qe.push({i, j});
  20.                     dist[i][j] = 0;
  21.                 }
  22.             }
  23.         }
  24.         if (qe.size() == 0 || qe.size() == n * m)
  25.             return -1; // 边界情况
  26.         int ans = 0;
  27.         while (qe.size())
  28.         {
  29.             auto [a, b] = qe.front();
  30.             qe.pop();
  31.             for (int i = 0; i < 4; i++)
  32.             {
  33.                 int x = dx[i] + a, y = dy[i] + b;
  34.                 if (x >= 0 && x < n && y >= 0 && y < m && dist[x][y] == -1)
  35.                 {
  36.                     dist[x][y] = dist[a][b] + 1;
  37.                     qe.push({x, y});
  38.                     ans = max(ans, dist[x][y]);
  39.                 }
  40.             }
  41.         }
  42.         return ans;
  43.     }
  44. };
  45. //统计遍历层数
  46. class Solution
  47. {
  48. public:
  49.     bool vis[100][100];
  50.     int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
  51.     int m, n, ret = -1;
  52.     int maxDistance(vector<vector<int>> &grid)
  53.     {
  54.         m = grid.size(), n = grid[0].size();
  55.         queue<pair<int, int>> q;
  56.         for (int i = 0; i < m; i++)
  57.         {
  58.             for (int j = 0; j < n; j++)
  59.             {
  60.                 if (grid[i][j] == 1)
  61.                 {
  62.                     q.push({i, j});
  63.                     vis[i][j] = true;
  64.                 }
  65.             }
  66.         }
  67.         if (q.size() == 0 || q.size() == m * n)
  68.             return ret;
  69.         while (q.size())
  70.         {
  71.             ret++;
  72.             int sz = q.size();
  73.             while (sz--)
  74.             {
  75.                 auto [a, b] = q.front();
  76.                 q.pop();
  77.                 for (int i = 0; i < 4; i++)
  78.                 {
  79.                     int x = a + dx[i], y = b + dy[i];
  80.                     if (x >= 0 && x < m && y >= 0 && y < n && !vis[x][y])
  81.                     {
  82.                         q.push({x, y});
  83.                         vis[x][y] = true;
  84.                     }
  85.                 }
  86.             }
  87.         }
  88.         return ret;
  89.     }
  90. };
复制代码
  以上便是全部内容,有题目欢迎在评论区指出,感谢观看!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/) Powered by Discuz! X3.4