LeetCode 面试题 16.19. 水域大小

一、题目

  你有一个用于表示一片土地的整数矩阵 land,该矩阵中每个点的值代表对应地点的海拔高度。若值为0则表示水域。由垂直、水平或对角连接的水域为池塘。池塘的大小是指相连接的水域的个数。编写一个方法来计算矩阵中所有池塘的大小,返回值需要从小到大排序。

示例:

输入:
[
[0,2,1,0],
[0,1,0,1],
[1,1,0,1],
[0,1,0,1]
]
输出: [1,2,4]

提示:

  • 0 < len(land) <= 1000
  • 0 < len(land[i]) <= 1000

  点击此处跳转题目

二、C# 题解

  遍历所有点,若为池塘,则进行泛洪“污染”(深度优先遍历),将与其连接的池塘全设置为 -1,以免后续重复计算。

  本算法使用 DIR 表示当前相对泛洪中心的位置,以确定泛洪的下次污染区域,减少些许重复“污染”时间。

public class Solution {
    
    
    public enum DIR // 表示当前 Flood 与中心的相对位置
    {
    
    
        Center, Up, Down, Left, Right,
        LeftUp, LeftDown, RightUp, RightDown,
    }

    public int[] PondSizes(int[][] land) {
    
    
        List<int> ans = new List<int>();
        for (var i = 0; i < land.Length; i++)
        for (var j = 0; j < land[i].Length; j++)
            if (land[i][j] == 0)
                ans.Add(Flood(land, i, j, DIR.Center));

        ans.Sort();
        return ans.ToArray();
    }

    // 泛洪算法,将位置 i,j 及其周围的区域依次 "污染"
    public int Flood(int[][] land, int i, int j, DIR d) {
    
    
        if (i < 0 || i >= land.Length) return 0;
        if (j < 0 || j >= land[0].Length) return 0;
        if (land[i][j] != 0) return 0;
        land[i][j] = -1;  // 进行 "污染"
        return d switch {
    
     // 依据相对位置进行泛洪
            DIR.Center => Flood(land, i - 1, j - 1, DIR.LeftUp) +
                          Flood(land, i - 1, j, DIR.Up) +
                          Flood(land, i - 1, j + 1, DIR.RightUp) +
                          Flood(land, i, j - 1, DIR.Left) +
                          Flood(land, i, j + 1, DIR.Right) +
                          Flood(land, i + 1, j - 1, DIR.LeftDown) +
                          Flood(land, i + 1, j, DIR.Down) +
                          Flood(land, i + 1, j + 1, DIR.RightDown) + 1,
            DIR.Up => Flood(land, i - 1, j - 1, DIR.LeftUp) +
                      Flood(land, i - 1, j, DIR.Up) +
                      Flood(land, i - 1, j + 1, DIR.RightUp) + 1,
            DIR.Down => Flood(land, i + 1, j - 1, DIR.LeftDown) +
                        Flood(land, i + 1, j, DIR.Down) +
                        Flood(land, i + 1, j + 1, DIR.RightDown) + 1,
            DIR.Left => Flood(land, i, j - 1, DIR.Left) +
                        Flood(land, i - 1, j - 1, DIR.LeftUp) +
                        Flood(land, i + 1, j - 1, DIR.LeftDown) + 1,
            DIR.Right => Flood(land, i, j + 1, DIR.Right) +
                         Flood(land, i - 1, j + 1, DIR.RightUp) +
                         Flood(land, i + 1, j + 1, DIR.RightDown) + 1,
            DIR.LeftUp => Flood(land, i - 1, j - 1, DIR.LeftUp) +
                          Flood(land, i - 1, j, DIR.Up) +
                          Flood(land, i - 1, j + 1, DIR.RightUp) +
                          Flood(land, i + 1, j - 1, DIR.LeftDown) +
                          Flood(land, i, j - 1, DIR.Left) + 1,
            DIR.LeftDown => Flood(land, i + 1, j - 1, DIR.LeftDown) +
                            Flood(land, i + 1, j, DIR.Down) +
                            Flood(land, i + 1, j + 1, DIR.RightDown) +
                            Flood(land, i, j - 1, DIR.Left) +
                            Flood(land, i - 1, j - 1, DIR.LeftUp) + 1,
            DIR.RightUp => Flood(land, i - 1, j, DIR.Up) +
                           Flood(land, i - 1, j - 1, DIR.LeftUp) +
                           Flood(land, i - 1, j + 1, DIR.RightUp) +
                           Flood(land, i, j + 1, DIR.Right) +
                           Flood(land, i + 1, j + 1, DIR.RightDown) + 1,
            DIR.RightDown => Flood(land, i + 1, j, DIR.Down) +
                             Flood(land, i + 1, j - 1, DIR.LeftDown) +
                             Flood(land, i + 1, j + 1, DIR.RightDown) +
                             Flood(land, i - 1, j + 1, DIR.RightUp) +
                             Flood(land, i, j + 1, DIR.Right) + 1,
            _ => throw new ArgumentOutOfRangeException(nameof(d), d, null)
        };
    }
}
  • 时间:240 ms,击败 100.00% 使用 C# 的用户
  • 内存:66.7 MB,击败 100.00% 使用 C# 的用户

猜你喜欢

转载自blog.csdn.net/zheliku/article/details/134342717