给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离。
两个相邻元素间的距离为 1 。
示例 1:
输入:
0 0 0
0 1 0
0 0 0
输出:
0 0 0
0 1 0
0 0 0
思路
BFS;通过viiste数组标记元素是否访问过;将元素为0全部入队;标记置为True;然后队列出队;上下左右四个方向进行判断;如果没有访问过;并且合法;说明该值为1,然后将该值标记为True,然后将该元素入队;
class Solution {
int[][] directions = new int[][]{{0,-1},{0,1},{1,0},{-1,0}};
public int[][] updateMatrix(int[][] matrix) {
if (matrix == null || matrix.length == 0) return matrix;
int n = matrix.length, m = matrix[0].length;
int[][] res = new int[n][m];
// 标记当前位置是否都看过
boolean[][] visited = new boolean[n][m];
// BFS 队列
Queue<int []> queue = new LinkedList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
// 将 0 全部放入队列
if (matrix[i][j] == 0) {
queue.offer(new int[]{i, j});
visited[i][j] = true;
}
}
}
while (!queue.isEmpty()){
int[] top = queue.poll();
int x = top[0], y = top[1];
// BFS 搜索四个方向
for (int[] direction : directions) {
int newX = x + direction[0];
int newY = y + direction[1];
if (newX < 0 || newX >= n || newY < 0 || newY >= m || visited[newX][newY]) continue;
res[newX][newY] = res[x][y] + 1; // 距离更新
visited[newX][newY] = true;
queue.add(new int[]{newX, newY}); // 新元素入队
}
}
return res;
}
}```