java:
bfs好难 还有之前的滑动窗口 都不太会 明天开始重新学习
都是看的题解 谢谢各位题解大佬
一天天不是烂橘子就是烂苹果
class Solution {
public int orangesRotting(int[][] grid) {
int m = grid.length;
int l = grid[0].length;
int count = 0;
Queue<int[]> queue = new LinkedList<>();
for(int r = 0; r < m; r++){
for(int c = 0; c < l; c++){
if(grid[r][c] == 1){
count++;
}else if(grid[r][c] == 2){
queue.add(new int[]{r,c});
}
}
}
int round = 0;
while(count > 0 && !queue.isEmpty()){
round ++;
int n = queue.size();
for(int i = 0; i < n; i++){
int[] orange = queue.poll();
int r = orange[0];
int c = orange[1];
if(r - 1 >= 0 && grid[r - 1][c] == 1){
grid[r - 1][c] = 2;
count--;
queue.add(new int[]{r-1,c});
}
if(r + 1 < m && grid[r + 1][c] == 1){
grid[r + 1][c] = 2;
count--;
queue.add(new int[]{r+1,c});
}
if(c - 1 >= 0 && grid[r][c - 1] == 1){
grid[r][c - 1] = 2;
count--;
queue.add(new int[]{r,c - 1});
}
if(c + 1 < l && grid[r][c + 1] == 1){
grid[r][c + 1] = 2;
count--;
queue.add(new int[]{r,c + 1});
}
}
}
if(count > 0){
return -1;
} else {
return round;
}
}
}