题目描述:994. 腐烂的橘子 - 力扣(LeetCode)
腐烂的橘子会污染周围的橘子,要求多少轮扩散才能把全部橘子污染,这就相当于滴墨水入清水,会扩散,其实就是广度遍历,看看遍历多少层可以遍历完可以遍历的
先遍历一次橘子,记录下腐烂橘子的位置和新鲜橘子的数目,然后广度遍历腐烂橘子并向外扩散污染新鲜橘子
注意向外扩散时需要每次取位置,因为移动会改变位置,位置需要重置
class Solution {
public:
int rows, columns;
vector<vector<int> > grid;
bool isValid(int x, int y) {
return x >= 0 && y >= 0 && x < rows && y < columns;
}
int orangesRotting(vector<vector<int> > &grid) {
rows = grid.size();
columns = grid[0].size();
this->grid = move(grid);
int ans = 0, fresh = 0;
queue<pair<int, int> > pullte;
for (int i = 0; i < rows; ++i)
for (int j = 0; j < columns; ++j)
if (th