多源BFS问题解决:用 BFS 解决边权为1的多源最短路问题
解法一:把多源最短路问题转化为若干个单源最短路问题
解法二:把所有的源点当成一个“超级源点”,从而转化为单源最短路问题(推荐)
单源最短路问题的解法:把起点加入到队列中,一层一层向外扩展
多源最短路问题的解法:把所有的起点加入到队列中,一层一层向外扩展
- 01 矩阵
class Solution {
int m, n;
vector<vector<int>> distance;
queue<pair<int, int>> q;
unordered_multimap<int, int> direction = {
{0, 1},
{0, -1},
{1, 0},
{-1, 0},
};
public:
void bfs() {
while (!q.empty()) {
auto front = q.front();
q.pop();
for (auto& e : direction) {
int x = front.first + e.first, y = front.second + e.second;
if (x >= 0 && x < m
&& y >= 0 && y < n
&& distance[x][y] == -1) {
distance[x][y] = distance[front.first][front.second] + 1;
q.push({x, y});
}
}
}
}
vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {
m = mat.size(), n = mat[0].size();
distance = vector<vector<int>>(m, vector<int>(n, -1));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (mat[i][j] == 0) {
distance[i][j] = 0;
q.push({i, j});
}
}
}
bfs();
return distance;
}
};
- 飞地的数量
class Solution {
int m, n;
vector<vector<bool>> mark;
queue<pair<int, int>> q;
unordered_multimap<int, int> direction = {
{0, 1},
{0, -1},
{1, 0},
{-1, 0},
};
public:
void bfs(vector<vector<int>>& grid) {
while (!q.empty()) {
auto front = q.front();
q.pop();
for (auto& e : direction) {
int x = front.first + e.first, y = front.second + e.second;
if (x >= 0 && x < m
&& y >= 0 && y < n
&& !mark[x][y]
&& grid[x][y] == 1) {
mark[x][y] = true;
q.push({x, y});
}
}
}
}
int numEnclaves(vector<vector<int>>& grid) {
m = grid.size(), n = grid[0].size();
mark = vector<vector<bool>>(m, vector<bool>(n, false));
// grid[i][0] 和 grid[i][n-1]
for (int i = 0; i < m; ++i) {
if (grid[i][0] == 1 && !mark[i][0]) {
mark[i][0] = true;
q.push({i, 0});
}
if (grid[i][n-1] == 1 && !mark[i][n-1]) {
mark[i][n-1] = true;
q.push({i, n-1});
}
}
// grid[0][j] 和 grid[m-1][j]
for (int j = 0; j < n; ++j) {
if (grid[0][j] == 1 && !mark[0][j]) {
mark[0][j] = true;
q.push({0, j});
}
if (grid[m-1][j] == 1 && !mark[m-1][j]) {
mark[m-1][j] = true;
q.push({m-1, j});
}
}
bfs(grid);
int ret = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == 1 && !mark[i][j]) {
++ret;
}
}
}
return ret;
}
};
- 地图中的最高点
class Solution {
int m, n;
vector<vector<int>> height;
queue<pair<int, int>> q;
unordered_multimap<int, int> direction = {
{0, 1},
{0, -1},
{1, 0},
{-1, 0},
};
public:
void bfs() {
while (!q.empty()) {
auto front = q.front();
q.pop();
for (auto& e : direction) {
int x = front.first + e.first, y = front.second + e.second;
if (x >= 0 && x < m
&& y >= 0 && y < n
&& height[x][y] == -1) {
height[x][y] = height[front.first][front.second] + 1;
q.push({x, y});
}
}
}
}
vector<vector<int>> highestPeak(vector<vector<int>>& isWater) {
m = isWater.size(), n = isWater[0].size();
height = vector<vector<int>>(m, vector<int>(n, -1));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (isWater[i][j]) {
height[i][j] = 0;
q.push({i, j});
}
}
}
bfs();
return height;
}
};
- 地图分析
class Solution {
int m, n;
vector<vector<int>> distance;
queue<pair<int, int>> q;
unordered_multimap<int, int> direction = {
{0, 1},
{0, -1},
{1, 0},
{-1, 0},
};
int ret = 0;
public:
void bfs() {
while (!q.empty()) {
auto front = q.front();
q.pop();
for (auto& e : direction) {
int x = front.first + e.first, y = front.second + e.second;
if (x >= 0 && x < m
&& y >= 0 && y < n
&& distance[x][y] == -1) {
distance[x][y] = distance[front.first][front.second] + 1;
q.push({x, y});
ret = max(ret, distance[x][y]);
}
}
}
}
int maxDistance(vector<vector<int>>& grid) {
m = grid.size(), n = grid[0].size();
distance = vector<vector<int>>(m, vector<int>(n, -1));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == 1) {
distance[i][j] = 0;
q.push({i, j});
}
}
}
if (q.empty() || q.size() == m * n) return -1;
bfs();
return ret;
}
};