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.
Example 1:
Input: grid = [[0,1,1,0],[0,1,1,0],[0,0,0,0]]
Output: 2
Explanation: 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.
Example 2:
Input: grid = [[1,1]]
Output: 2
Explanation: Grid of full water is also disconnected ([[1,1]] -> [[0,0]]), 0 islands.
Constraints:
- m == grid.length
- n == grid[i].length
- 1 <= m, n <= 30
- grid[i][j] is either 0 or 1.
想了半天, 让别人一句话给点醒了, 任何形状的陆地, 最多将 2 块地变成水就可以实现不联通的状态。
我们只需要检查有没有变 0 或者 1 块就可以实现的情况就可以了
要注意的是全是水的情况也叫不连通
use std::collections::HashSet;
impl Solution {
fn bfs(grid: &Vec<Vec<i32>>) -> i32 {
let mut count = 0;
let mut queue = Vec::new();
let mut visited = HashSet::new();
'outer: for r in 0..grid.len() {
for c in 0..grid[0].len() {
if grid[r][c] == 1 {
queue.push((r, c));
break 'outer;
}
}
}
while let Some((r, c)) = queue.pop() {
if visited.contains(&(r, c)) {
continue;
}
visited.insert((r, c));
count += 1;
if r > 0 && grid[r - 1][c] == 1 {
queue.push((r - 1, c));
}
if r < grid.len() - 1 && grid[r + 1][c] == 1 {
queue.push((r + 1, c));
}
if c > 0 && grid[r][c - 1] == 1 {
queue.push((r, c - 1));
}
if c < grid[0].len() - 1 && grid[r][c + 1] == 1 {
queue.push((r, c + 1));
}
}
count
}
pub fn min_days(mut grid: Vec<Vec<i32>>) -> i32 {
let mut num_of_land = 0;
for r in 0..grid.len() {
for c in 0..grid[0].len() {
num_of_land += grid[r][c];
}
}
if num_of_land == 0 {
return 0;
}
let count = Solution::bfs(&grid);
if count < num_of_land {
return 0;
}
for r in 0..grid.len() {
for c in 0..grid[0].len() {
if grid[r][c] == 1 {
grid[r][c] = 0;
let count = Solution::bfs(&grid);
if count < num_of_land - 1 || count == 0 {
return 1;
}
grid[r][c] = 1;
}
}
}
2
}
}