LeetCode-934. Shortest Bridgehttps://leetcode.com/problems/shortest-bridge/
题目描述
You are given an n x n
binary matrix grid
where 1
represents land and 0
represents water.
An island is a 4-directionally connected group of 1
's not connected to any other 1
's. There are exactly two islands in grid
.
You may change 0
's to 1
's to connect the two islands to form one island.
Return the smallest number of 0
's you must flip to connect the two islands.
Example 1:
Input: grid = [[0,1],[1,0]] Output: 1
Example 2:
Input: grid = [[0,1,0],[0,0,0],[0,0,1]] Output: 2
Example 3:
Input: grid = [[1,1,1,1,1],[1,0,0,0,1],[1,0,1,0,1],[1,0,0,0,1],[1,1,1,1,1]] Output: 1
Constraints:
n == grid.length == grid[i].length
2 <= n <= 100
grid[i][j]
is either0
or1
.- There are exactly two islands in
grid
.
解题思路
【C++】
DFS+BFS
class Solution {
vector<int> direction{-1, 0, 1, 0, -1};
public:
int shortestBridge(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
queue<pair<int, int>> points;
bool flipped = false;
for (int i = 0; i < m; ++i) {
if (flipped) {break;}
for (int j = 0; j < n; ++j) {
if (grid[i][j] == 1) {
dfs(points, grid, m, n, i, j);
flipped = true;
break;
}
}
}
int x, y;
int level = 0;
while (!points.empty()) {
++level;
int size = points.size();
while (size--) {
auto [r, c] = points.front();
points.pop();
for (int k = 0; k < 4; ++k) {
x = r + direction[k], y = c + direction[k+1];
if (x >= 0 && y >= 0 && x < m && y < n) {
if (grid[x][y] == 2) {
continue;
}
if (grid[x][y] == 1) {
return level;
}
points.push({x, y});
grid[x][y] = 2;
}
}
}
}
return 0;
}
void dfs(queue<pair<int, int>>& points, vector<vector<int>>& grid, int m, int n, int i, int j) {
if (i < 0 || j < 0 || i == m || j == n || grid[i][j] == 2) {return;}
if (grid[i][j] == 0) {
points.push({i, j});
return;
}
grid[i][j] = 2;
dfs(points, grid, m, n, i - 1, j);
dfs(points, grid, m, n, i + 1, j);
dfs(points, grid, m, n, i, j - 1);
dfs(points, grid, m, n, i, j + 1);
}
};
【Java】
class Solution {
public int shortestBridge(int[][] grid) {
int m = grid.length, n = grid[0].length;
int[] direction = new int[]{-1, 0, 1, 0, -1};
Queue<int[]> points = new LinkedList<>();
boolean flipped = false;
for (int i = 0; i < m; ++i) {
if (flipped) {break;}
for (int j = 0; j < n; ++j) {
if (grid[i][j] == 1) {
dfs(points, grid, m, n, i, j);
flipped = true;
break;
}
}
}
int x, y;
int level = 0;
while (!points.isEmpty()) {
++level;
int size = points.size();
while (size-- > 0) {
int[] rc = points.poll();
for (int k = 0; k < 4; ++k) {
x = rc[0] + direction[k]; y = rc[1] + direction[k+1];
if (x >= 0 && y >= 0 && x < m && y < n) {
if (grid[x][y] == 2) {
continue;
}
if (grid[x][y] == 1) {
return level;
}
points.add(new int[]{x, y});
grid[x][y] = 2;
}
}
}
}
return 0;
}
void dfs(Queue<int[]> points, int[][] grid, int m, int n, int i, int j) {
if (i < 0 || j < 0 || i == m || j == n || grid[i][j] == 2) {return;}
if (grid[i][j] == 0) {
points.add(new int[]{i, j});
return;
}
grid[i][j] = 2;
dfs(points, grid, m, n, i - 1, j);
dfs(points, grid, m, n, i + 1, j);
dfs(points, grid, m, n, i, j - 1);
dfs(points, grid, m, n, i, j + 1);
}
}