You are given an m x n binary grid grid where 1 represents land and 0 represents water. An island is a maximal 4-directionally (horizontal or vertical) connected group of 1’s.
The grid is said to be connected if we have exactly one island, otherwise is said disconnected.
In one day, we are allowed to change any single land cell (1) into a water cell (0).
Return the minimum number of days to disconnect the grid.
Input: grid =[[0,1,1,0],[0,1,1,0],[0,0,0,0]]Output: 2Explanation: We need at least 2 days to get a disconnected grid.Change land grid[1][1] and grid[0][2] to water and get 2 disconnected island.
Check if the grid is already disconnected: If the grid has no land or more than one island, it is already disconnected, hence return 0.
Check if removing one land cell will disconnect the grid immediately: Iterate through each land cell, temporarily remove it, and check the connectivity of the grid. If removing any single land cell results in more than one island, return 1.
If not, return 2: If the above conditions are not met, then the minimum number of days required to disconnect the grid will be 2 under all other circumstances.
publicclassSolution {
privatestaticfinalint[][] DIRS = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
publicintminDays(int[][] grid) {
if (!isConnected(grid)) {
return 0;
}
int m = grid.length;
int n = grid[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j]== 1) {
grid[i][j]= 0; // Temporarily remove the landif (!isConnected(grid)) {
return 1;
}
grid[i][j]= 1; // Restore the land }
}
}
return 2;
}
privatebooleanisConnected(int[][] grid) {
int m = grid.length;
int n = grid[0].length;
int[] start = findStart(grid);
if (start ==null) {
returnfalse; // No land found }
boolean[][] visited =newboolean[m][n];
// try to visit whole island from starting point dfs(grid, visited, start[0], start[1]);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
// if some cell is unvisited, island is disconnectedif (grid[i][j]== 1 &&!visited[i][j]) {
returnfalse;
}
}
}
returntrue;
}
privateint[]findStart(int[][] grid) {
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j]== 1) {
returnnewint[]{i, j};
}
}
}
returnnull;
}
privatevoiddfs(int[][] grid, boolean[][] visited, int r, int c) {
if (r < 0 || r >= grid.length|| c < 0 || c >= grid[0].length|| visited[r][c]|| grid[r][c]== 0) {
return;
}
visited[r][c]=true;
for (int i = 0; i < 4; i++) {
int newR = r + DIRS[i][0];
int newC = c + DIRS[i][1];
dfs(grid, visited, newR, newC);
}
}
}
defminDays(grid):
defis_connected(grid):
rows, cols = len(grid), len(grid[0])
visited = [[False] * cols for _ in range(rows)]
defdfs(x, y):
if (
x <0or x >= rows
or y <0or y >= cols
or visited[x][y]
or grid[x][y] ==0 ):
return visited[x][y] =Truefor dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
dfs(x + dx, y + dy)
# Find the first land cell and start DFS from there start =Nonefor i in range(rows):
for j in range(cols):
if grid[i][j] ==1:
start = (i, j)
breakif start:
breakifnot start:
returnFalse# No land found dfs(start[0], start[1])
for i in range(rows):
for j in range(cols):
if grid[i][j] ==1andnot visited[i][j]:
returnFalse# Found unvisited land meaning disconnectedreturnTrue# Initial check if already disconnectedifnot is_connected(grid):
return0# Try to disconnect by removing one land cell rows, cols = len(grid), len(grid[0])
for i in range(rows):
for j in range(cols):
if grid[i][j] ==1:
grid[i][j] =0ifnot is_connected(grid):
return1 grid[i][j] =1# Restore the land cell# If can't be disconnected by removing one cell, return 2return2