
Input: grid =[[1,1,0],[0,1,1],[1,1,1]]Output: 3Explanation: Use 3 operations to change grid[0][1], grid[1][2], and grid[2][1] to 0.After, no more 1's are 4-directionally connected and grid is well-isolated.
Example 2:
1
2
3
4
5

Input: grid =[[0,0,0],[0,0,0],[0,0,0]]Output: 0Explanation: There are no 1's in grid and it is well-isolated.No operations were done so return0.
Example 3:
1
2
3
4
5

Input: grid =[[0,1],[1,0]]Output: 0Explanation: None of the 1's are 4-directionally connected and grid is well-isolated.No operations were done so return0.
Each pair of adjacent 1’s must be broken by flipping at least one of them. The minimum number of flips is the size of the minimum vertex cover in the graph formed by adjacent 1’s, which equals the size of the maximum matching (König’s theorem for bipartite graphs). Color the grid in a chessboard pattern and build the bipartite graph, then use DFS to find the maximum matching.
#include<vector>classSolution {
public:int minimumOperations(std::vector<std::vector<int>>& grid) {
int m = grid.size(), n = grid[0].size(), res =0;
std::vector<std::vector<int>> match(m, std::vector<int>(n, -1));
std::vector<std::vector<int>> vis(m, std::vector<int>(n, 0));
std::vector<int> dx{0,0,1,-1}, dy{1,-1,0,0};
auto dfs = [&](auto&& self, int x, int y, int t) ->bool {
for (int d =0; d <4; ++d) {
int nx = x+dx[d], ny = y+dy[d];
if (nx<0||ny<0||nx>=m||ny>=n||grid[nx][ny]!=1) continue;
if (vis[nx][ny]==t) continue;
vis[nx][ny]=t;
if (match[nx][ny]==-1||self(self,match[nx][ny]/n,match[nx][ny]%n,t)) {
match[nx][ny]=x*n+y;
return true;
}
}
return false;
};
int t=1;
for (int i=0;i<m;++i) for (int j=0;j<n;++j) if (grid[i][j]==1&&(i+j)%2==0) {
if (dfs(dfs,i,j,t++)) ++res;
}
return res;
}
};
1
2
// For each black cell, try to match with adjacent white cells using DFS.// Pseudocode only, as Go is not supported for this graph matching on Leetcode.
classSolution {
int m, n;
int[][] match, vis;
int[] dx = {0,0,1,-1}, dy = {1,-1,0,0};
publicintminimumOperations(int[][] grid) {
m = grid.length; n = grid[0].length;
match =newint[m][n]; vis =newint[m][n];
for (int[] row : match) Arrays.fill(row, -1);
int res = 0, t = 1;
for (int i=0;i<m;i++) for (int j=0;j<n;j++) if (grid[i][j]==1&&(i+j)%2==0) {
if (dfs(grid,i,j,t++)) res++;
}
return res;
}
booleandfs(int[][] grid, int x, int y, int t) {
for (int d=0;d<4;d++) {
int nx=x+dx[d], ny=y+dy[d];
if (nx<0||ny<0||nx>=m||ny>=n||grid[nx][ny]!=1) continue;
if (vis[nx][ny]==t) continue;
vis[nx][ny]=t;
if (match[nx][ny]==-1||dfs(grid,match[nx][ny]/n,match[nx][ny]%n,t)) {
match[nx][ny]=x*n+y;
returntrue;
}
}
returnfalse;
}
}
1
2
// For each black cell, try to match with adjacent white cells using DFS.
// Pseudocode only, as Kotlin is not supported for this graph matching on Leetcode.
classSolution:
defminimumOperations(self, grid: list[list[int]]) -> int:
m, n = len(grid), len(grid[0])
match= [[-1]*n for _ in range(m)]
vis = [[0]*n for _ in range(m)]
dx, dy = [0,0,1,-1], [1,-1,0,0]
defdfs(x, y, t):
for d in range(4):
nx, ny = x+dx[d], y+dy[d]
if nx<0or ny<0or nx>=m or ny>=n or grid[nx][ny]!=1: continueif vis[nx][ny]==t: continue vis[nx][ny]=t
ifmatch[nx][ny]==-1or dfs(match[nx][ny]//n, match[nx][ny]%n, t):
match[nx][ny]=x*n+y
returnTruereturnFalse res, t =0, 1for i in range(m):
for j in range(n):
if grid[i][j]==1and (i+j)%2==0:
if dfs(i,j,t):
res +=1 t +=1return res
1
2
// For each black cell, try to match with adjacent white cells using DFS.
// Pseudocode only, as Rust is not supported for this graph matching on Leetcode.
1
2
// For each black cell, try to match with adjacent white cells using DFS.
// Pseudocode only, as TypeScript is not supported for this graph matching on Leetcode.