返回介绍

solution / 1500-1599 / 1568.Minimum Number of Days to Disconnect Island / README_EN

发布于 2024-06-17 01:03:17 字数 6920 浏览 0 评论 0 收藏 0

1568. Minimum Number of Days to Disconnect Island

中文文档

Description

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.

Solutions

Solution 1

class Solution:
  def minDays(self, grid: List[List[int]]) -> int:
    if self.count(grid) != 1:
      return 0
    m, n = len(grid), len(grid[0])
    for i in range(m):
      for j in range(n):
        if grid[i][j] == 1:
          grid[i][j] = 0
          if self.count(grid) != 1:
            return 1
          grid[i][j] = 1
    return 2

  def count(self, grid):
    def dfs(i, j):
      grid[i][j] = 2
      for a, b in [[0, -1], [0, 1], [1, 0], [-1, 0]]:
        x, y = i + a, j + b
        if 0 <= x < m and 0 <= y < n and grid[x][y] == 1:
          dfs(x, y)

    m, n = len(grid), len(grid[0])
    cnt = 0
    for i in range(m):
      for j in range(n):
        if grid[i][j] == 1:
          dfs(i, j)
          cnt += 1
    for i in range(m):
      for j in range(n):
        if grid[i][j] == 2:
          grid[i][j] = 1
    return cnt
class Solution {
  private static final int[] DIRS = new int[] {-1, 0, 1, 0, -1};
  private int[][] grid;
  private int m;
  private int n;

  public int minDays(int[][] grid) {
    this.grid = grid;
    m = grid.length;
    n = grid[0].length;
    if (count() != 1) {
      return 0;
    }
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (grid[i][j] == 1) {
          grid[i][j] = 0;
          if (count() != 1) {
            return 1;
          }
          grid[i][j] = 1;
        }
      }
    }
    return 2;
  }

  private int count() {
    int cnt = 0;
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (grid[i][j] == 1) {
          dfs(i, j);
          ++cnt;
        }
      }
    }
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (grid[i][j] == 2) {
          grid[i][j] = 1;
        }
      }
    }
    return cnt;
  }

  private void dfs(int i, int j) {
    grid[i][j] = 2;
    for (int k = 0; k < 4; ++k) {
      int x = i + DIRS[k], y = j + DIRS[k + 1];
      if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1) {
        dfs(x, y);
      }
    }
  }
}
class Solution {
public:
  const vector<int> dirs = {-1, 0, 1, 0, -1};
  int m, n;

  int minDays(vector<vector<int>>& grid) {
    m = grid.size(), n = grid[0].size();
    if (count(grid) != 1) {
      return 0;
    }
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (grid[i][j] == 1) {
          grid[i][j] = 0;
          if (count(grid) != 1) {
            return 1;
          }
          grid[i][j] = 1;
        }
      }
    }
    return 2;
  }

  int count(vector<vector<int>>& grid) {
    int cnt = 0;
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (grid[i][j] == 1) {
          dfs(i, j, grid);
          ++cnt;
        }
      }
    }
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (grid[i][j] == 2) {
          grid[i][j] = 1;
        }
      }
    }
    return cnt;
  }

  void dfs(int i, int j, vector<vector<int>>& grid) {
    grid[i][j] = 2;
    for (int k = 0; k < 4; ++k) {
      int x = i + dirs[k], y = j + dirs[k + 1];
      if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1) {
        dfs(x, y, grid);
      }
    }
  }
};
func minDays(grid [][]int) int {
  m, n := len(grid), len(grid[0])
  dirs := []int{-1, 0, 1, 0, -1}

  var dfs func(i, j int)
  dfs = func(i, j int) {
    grid[i][j] = 2
    for k := 0; k < 4; k++ {
      x, y := i+dirs[k], j+dirs[k+1]
      if x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 {
        dfs(x, y)
      }
    }
  }

  count := func() int {
    cnt := 0
    for i := 0; i < m; i++ {
      for j := 0; j < n; j++ {
        if grid[i][j] == 1 {
          dfs(i, j)
          cnt++
        }
      }
    }
    for i := 0; i < m; i++ {
      for j := 0; j < n; j++ {
        if grid[i][j] == 2 {
          grid[i][j] = 1
        }
      }
    }
    return cnt
  }

  if count() != 1 {
    return 0
  }
  for i := 0; i < m; i++ {
    for j := 0; j < n; j++ {
      if grid[i][j] == 1 {
        grid[i][j] = 0
        if count() != 1 {
          return 1
        }
        grid[i][j] = 1
      }
    }
  }
  return 2
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文