0.多源最短路问题(BFS默认是边权为1)


1.01矩阵
542. 01 矩阵 - 力扣(LeetCode)


class Solution {
int[] dx = { 0, 0, -1, 1 };
int[] dy = { 1, -1, 0, 0 };
public int[][] updateMatrix(int[][] mat) {
int m = mat.length, n = mat[0].length;
// dist[i][j] == -1:标记当前位置没有被搜索过
// dist[i][j] != -1:存的是最短距离
int[][] dist = new int[m][n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
dist[i][j] = -1;
Queue<int[]> q = new LinkedList<>();
// 1. 把所有的源点加⼊到队列⾥⾯
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++) {
if (mat[i][j] == 0) {
dist[i][j] = 0;
q.add(new int[] { i, j });