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# 的用户
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蔗理苦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值