java:
继续看题解学习bfs 慢慢学咯
class Solution {
public int[][] updateMatrix(int[][] mat) {
int m = mat.length;
int n =mat[0].length;
Queue queue = new linkedList<>();
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(mat[i][j] == 0){
queue.offer(new int[]{i,j});
}else{
mat[i][j] = -1;
}
}
}
int[] dx = new int[] {-1, 1, 0, 0};
int[] dy = new int[] {0, 0, -1, 1};
while(!queue.isEmpty()){
int[] point = queue.poll();
int x = point[0];
int y = point[1];
for(int i = 0; i < 4; i++){
int xx = x + dx[i];
int yy = y + dy[i];
if(xx >= 0 && xx < m && yy >=0 && yy < n && mat[xx][yy] == -1){
mat[xx][yy] = mat[x][y] + 1;
queue.offer(new int[]{xx,yy});
}
}
}
return mat;
}
}



