资讯专栏INFORMATION COLUMN

王者编程大赛之二 — 蓄水池

Me_Kun / 2878人阅读

摘要:相关文章王者编程大赛之一王者编程大赛之三背包王者编程大赛之四约瑟夫环王者编程大赛之五最短路径

首发于 樊浩柏科学院

自如寓打算门口用砖头围立一个蓄水池子,从上面看凹凸不平,凹的地方会有积水。那如果用数字代表每个砖头的高度,就形成一个二维数据(如示例),请问这个池子能存储多少单位的水?

例如二维数组为:

9 9 9 9
3 0 0 9
7 8 2 6
时,答案是中间的 0,0 位置可以存储 2(因为其外面最低是 2)个单位的水,因此答案为 2 + 2 = 4。

示例:
输入:[1 1 1 1,1 0 0 1,1 1 1 1]
输出:2
输入:[12 11 12 0 13,12 9 8 12 12,13 10 0 3 15,19 4 4 7 15,19 4 3 0 15,12 13 10 15 13]
输出:58

解题思路

这道题是所有题中困惑我时间最长的题,一开始思维禁锢在想直接通过找到每块砖的四周有效最低砖高度 $H_{min}$,然后这块砖所剩的水为 $w[i][j] = H_{min}-h[i][j]$($h[i][j]$ 为砖的高度,i 和 j 为砖的位置坐标),因此蓄水池能蓄下的水为 $sum_{i=1}^nsum_{j=1}^n w[i][j]$。经过一番尝试,发现寻找某块砖四周最低有效砖逻辑比较复杂,且不易理解,又尝试过使用回溯算法寻找出池子中的所有连通图,但是也未有果。

最后,发现基础平台一位同学的实现思路很清晰,我认为他的实现是最合适的,所以研究了一下。该实现中机智地采用逆向思维,首先往池子注满水(最高砖的高度),然后再通过条件判定每块砖是否需要进行漏水,一直到没有砖需要进行漏水操作。

实现思路如下:

找出高度最高的砖,高度记为 $H_{max}$;

对除去边界的砖进行注水操作,每块砖加水量为 $w[i][j] = H_{max} - h[i][j]$($h[i][j]$ 为砖的高度);

对某块砖进行漏水操作,只要这块砖有盛水且上下左右相邻的 4 块砖高度和盛水量之和小于这块砖高度和盛水量之和,则需要进行一次漏水,漏水条件可以描述为 $w[i][j] > 0$ && $h[i][j-1] + w[i][j-1] < h[i][j] + w[i][j]$(该条件为砖左侧相邻的漏水条件,右、上、下同理可得);

持续漏水操作,一直重复步骤 3 直至没有砖需要进行漏水操作;

求和砖的盛水量,$sum_{i=1}^nsum_{j=1}^n w[i][j]$ 即为水池的蓄水量;

算法流程图示如下:

编码实现

实现的类结构如下,特殊的方法已提取出,并将一一详细说明。

row = count($data);
        $this->col = count($data[0]);

        foreach ($data as $row => $rowArray) {
            foreach ($rowArray as $col => $height) {
                $height = (int)$height;
                $this->gridArray[$row][$col]["height"] = $height;
                $this->gridArray[$row][$col]["water"] = 0;
                //获取最高砖的高度
                if ($this->maxHeight < $height) {
                    $this->maxHeight = $height;
                }
            }
        }
    }
    
    //判断是否是水池边界
    public function isBorder($row, $col)
    {
        if ($row == 0
            || $row == $this->row - 1
            || $col == 0
            || $col == $this->col - 1
        ) {
            return true;
        }

        return false;
    }

    public function run()
    {
        $this->addWater();

        while ($this->removeWater()) ;

        return $this->collect();
    }
}

注水操作:

public function addWater()
{
    foreach ($this->gridArray as $row => $rowArray) {
        foreach ($rowArray as $col => $grid) {
            if (!$this->isBorder($row, $col)) {
                $this->gridArray[$row][$col]["water"] = $this->maxHeight - $this->gridArray[$row][$col]["height"];
            }
        }
    }
}

漏水操作:

public function removeWater()
{
    foreach ($this->gridArray as $row => $rowArray) {
        foreach ($rowArray as $col => $grid) {
            if ($this->canRemove($row, $col)) {
                return true;
            }
        }
    }

    return false;
}

漏水条件实现如下:

public function canRemove($row, $col)
{
    $can = false;

    if ($this->gridArray[$row][$col]["water"] > 0) {
        //上
        if ($this->gridArray[$row][$col]["water"] + $this->gridArray[$row][$col]["height"] >
            $this->gridArray[$row - 1][$col]["water"] + $this->gridArray[$row - 1][$col]["height"]) {
            $this->gridArray[$row][$col]["water"] =
                $this->gridArray[$row - 1][$col]["water"] + $this->gridArray[$row - 1][$col]["height"]
                - $this->gridArray[$row][$col]["height"];
            if ($this->gridArray[$row][$col]["water"] < 0) {
                $this->gridArray[$row][$col]["water"] = 0;
            }
            $can = true;
        }
        //右
        if ($this->gridArray[$row][$col]["water"] + $this->gridArray[$row][$col]["height"] >
            $this->gridArray[$row][$col + 1]["water"] + $this->gridArray[$row][$col + 1]["height"]) {
            $this->gridArray[$row][$col]["water"] =
                $this->gridArray[$row][$col + 1]["water"] + $this->gridArray[$row][$col + 1]["height"]
                - $this->gridArray[$row][$col]["height"];
            if ($this->gridArray[$row][$col]["water"] < 0) {
                $this->gridArray[$row][$col]["water"] = 0;
            }
            $can = true;
        }
        //下
        if ($this->gridArray[$row][$col]["water"] + $this->gridArray[$row][$col]["height"] >
            $this->gridArray[$row + 1][$col]["water"] + $this->gridArray[$row + 1][$col]["height"]) {
            $this->gridArray[$row][$col]["water"] =
                $this->gridArray[$row + 1][$col]["water"] + $this->gridArray[$row + 1][$col]["height"]
                - $this->gridArray[$row][$col]["height"];
            if ($this->gridArray[$row][$col]["water"] < 0) {
                $this->gridArray[$row][$col]["water"] = 0;
            }
            $can = true;
        }
        //左
        if ($this->gridArray[$row][$col]["water"] + $this->gridArray[$row][$col]["height"] >
            $this->gridArray[$row][$col - 1]["water"] + $this->gridArray[$row][$col - 1]["height"]) {
            $this->gridArray[$row][$col]["water"] =
                $this->gridArray[$row][$col - 1]["water"] + $this->gridArray[$row][$col - 1]["height"]
                - $this->gridArray[$row][$col]["height"];
            if ($this->gridArray[$row][$col]["water"] < 0) {
                $this->gridArray[$row][$col]["water"] = 0;
            }
            $can = true;
        }
    }

    return $can;
}

持续漏水操作:

public function run()
{
    while ($this->removeWater()) ;
}

求和砖的盛水量:

public function collect()
{
    $sum = 0;
    foreach ($this->gridArray as $row => $rowArray) {
        foreach ($rowArray as $col => $grid) {
            $sum += $grid["water"];
        }
    }

    return $sum;
}

接收标准输入处理并输出结果:

$filter = function ($value) {
    return explode(" ", $value);
};

$pool = new Pool(array_map($filter, explode(",", $input)));
echo $pool->run(), PHP_EOL;
相似题目

Twitter 之前曾经出过类似蓄水池的笔试题,只不过本题是立体水池(二维数组),Twitter 蓄水池笔试题是平面水池(一维数组),解题复杂度也就降低了,当然 Twitter 蓄水池笔试题也可以采用本题的思想来实现,但是时间复杂度为 $O(n^2)$,采用 我的Twitter技术面试失败了 的实现时间复杂度为 $O(n)$。

实现思路如下:

首先,用两个指针(left 和 right)分别指向数组的第一个元素和最后一个元素,左指针从左向右遍历,右指针从右向左遍历;

初始化数组中一个元素(a[0])为左边遍历得到的最大值(max_left),最后一个元素(a[a.length-1])为从右边遍历得到的最大值(max_right);

开始遍历,遍历结束条件为 左指针不小于右指针;

如果左边遍历的最大值小于右边遍历的最大值,说明只要有水沟(即小于左边最大值 max_left 的元素)就会有积水,因为右边的最大值可以保证左边水沟的积水不会流失掉;同样,如果左边遍历的最大值不小于右边遍历的最大值,只要右边有水沟(即小于右边最大值 max_right 的元素)就会有积水;

具体实现,请直接参考 CuGBabyBeaR 文章。

总结

本题的蓄水池问题,如果理解了问题本质并逆向思维,将寻找某块砖四周最低有效砖高度(寻找有效砖涉及到边界扩散)转化为判断某块砖是否需要漏水条件,那么问题就简化很多了,那后续编码也就很容易实现了,本文算法的时间复杂度为 $O(n^3)$。

相关文章 »

王者编程大赛之一(2017-12-05)

[王者编程大赛之三 — 01背包](https://www.fanhaobai.com/201...

(2017-12-05)

王者编程大赛之四 — 约瑟夫环(2017-12-06)

王者编程大赛之五 — 最短路径(2017-12-06)

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/31076.html

相关文章

  • 王者编程大赛之三 — 01背包

    摘要:动态规划概念动态规划过程每次决策依赖于当前状态,又随即引起状态的转移。相关文章王者编程大赛之一王者编程大赛之二蓄水池王者编程大赛之四约瑟夫环王者编程大赛之五最短路径 首发于 樊浩柏科学院 服务目前每月会对搬家师傅进行评级,根据师傅的评级排名结果,我们将优先保证最优师傅的全天订单。 showImg(https://img3.fanhaobai.com/2017/12/2017-ziro...

    Cympros 评论0 收藏0
  • 王者编程大赛之五 — 最短路径

    摘要:由于是从顶点到的最短路径,则有。算法流程根据最短路径的最优子结构性质,提出了以最短路径长度递增,逐次生成最短路径的算法。相关文章王者编程大赛之一王者编程大赛之二蓄水池王者编程大赛之三背包王者编程大赛之四约瑟夫环 首发于 樊浩柏科学院 自如年底就会拥有 50W 间房子,大家知道每间房房子都是需要配置完才能出租给自如客的,整个房租的配置过程是很复杂的,每天都需要大量的物流师傅将家电、家具...

    yuanzhanghu 评论0 收藏0
  • 王者编程大赛之一

    摘要:首发于樊浩柏科学院本次王者编程大赛分为个组别,分别为研发测试移动战场。本章只叙述前道相对简单的题目,后续题目及解题思路将在王者编程大赛系列中列出。 首发于 樊浩柏科学院 本次王者编程大赛分为 3 个组别,分别为研发、测试、移动战场。这里只讨论研发战场所考的 题目,本次大赛共有 7 道题,主要考查点为基础算法,解题所用语言不做限制,但是需要在 在线验证平台 使用标准输入并验证通过,最后...

    justCoding 评论0 收藏0
  • JavaScript面向对象编程学习笔记---概念定义

    摘要:子类继承自父类的方法可以重新定义即覆写,被调用时会使用子类定义的方法什么是多态青蛙是一个对象,金鱼也是一个对象,青蛙会跳,金鱼会游,定义好对象及其方法后,我们能用青蛙对象调用跳这个方法,也能用金鱼对象调用游这个方法。 1、专用术语 面向对象编程程序设计简称:OOP,在面向对象编程中常用到的概念有:对象、属性、方法、类、封装、聚合、重用与继承、多态。 2、什么是对象? 面向对象编程的重点...

    mikasa 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<