Php algorithm-- catching water droplets

php algorithm-- catching water droplets

A novice PHP programmer, who has been on the job for half a year and is still hovering in endless additions, deletions, modifications and data optimization, has recently been brought into the algorithm pit by the front-end brother and has done a lot of algorithm problems.
this question comes from the collar buckle network, which is more interesting.
given a matrix of m x n, in which the values are all positive integers, representing the height of each cell in the two-dimensional height map, please calculate the maximum volume of Rain Water in the figure.

description:

m an dna re integers less than 110. The height of each unit is greater than 0 and less than 20000.

example:

the height map of 3x6 is given as follows:
[
[1 br > [1 br 4],
[3 br 2],
[3 br >]

[3, 2, 3, 3, 2, 1]

[3, 2, 3, 3, 2, 1]

]

[3, 2, 3, 3, 2, 1]

returns 4.

[[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]]

after it rains, Rain Water will be stored in these squares. The total amount of rain water received is 4.

my idea is:
the volume that can receive water is the volume of each layer that is vacant, but some of the volume of the vacancy is notched. So the specific idea:

1.
2.
3. = -
4.

I don"t know if there is a better idea.


  1. cut off useless tall poles, such as two 4s;
  2. final state is that all the middle points are the highest points, for example, the middle four are all 3;
  3. the final state can be subtracted from the initial state.

class Trapping
{

public function water(){
    $arr = [
        [1,4,3,1,3,2],
        [3,2,1,3,2,4],
        [2,3,3,2,3,1]
    ];
    $shui = 0;
    //
    //$max
    $b = [];
    foreach($arr as $key=>$value){
        $a=$arr[$key];
        $b = array_merge($a,$b);
        sort($b);
    }
    $max = $b[count($b)-1];

    for($i=0;$i<=$max-1;$iPP){
        $eacharr = [];
        //
        foreach($arr as $key =>$row){
            foreach($row as $key1=>$row1){
                if($row1-$i<0){
                    $row1 =0;
                }else{
                    $row1 = $row1-$i;
                }
                $eacharr[$key][$key1] = $row1;
            }
        } 
        //       
        $shui += $this->each($eacharr);
    }
    return $shui;
}
/**
 * 
 * @param  [array] $arr []
 * @return [int]      []
 */
public function each($arr){
    $shui = 0;
    //
    foreach ($arr as $i => $row) {
        foreach($row as $j =>$row1){
            //$w 0
            $w = $arr[$i][$j];
            if($w==0){
                //
                if( $i==0||$j == 0||$i == count($arr)-1||$j == count($row)-1){
                    $arr[$i][$j] = 'kou';
                    //
                    $arr =  $this->kou($arr,$i,$j);
                }
            } 
        }
    }
    //shu
    foreach ($arr as $i => $row) {
        foreach($row as $j =>$row1){
            $w = $arr[$i][$j];
            if($w===0){
                $shuiPP;
            }  
        }
    }
    return $shui;
}
/**
 * 
 * @param  [array]  $arr []
 * @param  integer $i   []
 * @param  integer $j   []
 * @return [array]  $arr  []
 */
public function kou($arr,$i,$j){
        if(isset($arr[$i+1][$j])&&$arr[$i+1][$j]===0){
            $arr[$i+1][$j] = 'kou';
            $arr = $this->kou($arr,$i+1,$j);
        }elseif(isset($arr[$i-1][$j])&&$arr[$i-1][$j]===0){
            $arr[$i-1][$j] = 'kou';
            $arr =  $this->kou($arr,$i-1,$j);
        }elseif(isset($arr[$i][$j+1])&&$arr[$i][$j+1]===0){
            $arr[$i][$j+1] = 'kou';
            $arr = $this->kou($arr,$i,$j+1);
        }elseif(isset($arr[$i][$j-1])&&$arr[$i][$j-1]===0){
            $arr[$i][$j-1] = 'kou';
            $arr = $this->kou($arr,$i,$j-1);
        }else{
            return $arr;   
        } 
        return $arr;           
        
}

}

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e4391e-44a5b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e4391e-44a5b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?