题解 | #腐烂的苹果#
腐烂的苹果
https://www.nowcoder.com/practice/54ab9865ce7a45968b126d6968a77f34
class Solution {
private:
int row, col; // 记录矩阵的行、列
int dx[4] = { 0, 0, -1, 1 }; // 按照上、下、左、右的顺序BFS
int dy[4] = { 1, -1, 0, 0 };
bool flags[1010][1010] = { false };// 用于记录好苹果是否被传染
public:
int rotApple(vector<vector<int> >& grid) {
row = grid.size(), col = grid[0].size();
// 第一,遍历矩阵,将所有原点入队列
queue<pair<int, int>> q;
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
if (grid[i][j] == 2) {
q.push({i, j});
}
}
}
// 第二,BFS,层数-1就是分钟数
int cnt = 0;
while (!q.empty()) {
int sz = q.size(); // 记录每一层的出队个数
++cnt; // 层数++
while (sz--) {
auto [a, b] = q.front(); // C++17的语法,总之就是获取横纵坐标
q.pop();
for (int i = 0; i < 4; ++i) {
int x = a+dx[i];
int y = b+dy[i];
// 未越界的被传染的好苹果入队
if (x >= 0 && x < row && y >= 0 && y < col
&& !flags[x][y] && grid[x][y] == 1) {
flags[x][y] = true;
q.push({x, y});
}
}
}
}
// 第三,再遍历一遍观察是否有好苹果
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
if (!flags[i][j] && grid[i][j] == 1) {
return -1;
}
}
}
return cnt-1;
}
};

查看12道真题和解析