Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 跟踪看起来像金字塔的数字数组的最大路径_Php - Fatal编程技术网

Php 跟踪看起来像金字塔的数字数组的最大路径

Php 跟踪看起来像金字塔的数字数组的最大路径,php,Php,我需要创建一个函数来查找三角形中的最大总数,也就是一个显示位置的随机数字数组 我做了以下工作: $number=array(); $level = $_POST['line']; for ($y = 1; $y <= $level; $y ++) { for ($x = 1; $x <= $y; $x ++) { $number[$y][$x] = rand(1,9); }

我需要创建一个函数来查找三角形中的最大总数,也就是一个显示位置的随机数字数组

我做了以下工作:

    $number=array();
    $level = $_POST['line'];
    for ($y = 1; $y <= $level; $y ++)
    {
        for ($x = 1; $x <= $y; $x ++)
        {
            $number[$y][$x] = rand(1,9);    
        }
    }

    $path=maxPathSum($number,$level);
    var_dump($path);
}
function maxPathSum($tri, $m) 
{ 
    for ( $i = $m - 1; $i > 0; $i--) 
    {
        for ($j = 1; $j <= $i; $j++) 
        { 
            if ($tri[$i + 1][$j] > $tri[$i + 1][$j + 1]) 
            {
                $tri[$i][$j] += $tri[$i + 1][$j];
                $pos=array("x" => $i , "y" => $j);
            }
            else
            {
                $tri[$i][$j] += $tri[$i + 1][$j + 1];
                $pos=array("x" => $i+1 , "y" => $j+1); 
            }
        } 
        $path[]=$pos;
    } 
    $path[]=array("x"=>1,"y"=>1);
    var_dump($path);
    return $triangle[1][1];
} 
我是这样做的:)

函数maxPathSum($triangle,$m)
{ 
$clone_数组=$triangle;
对于($i=$m-1;$i>0;$i--)
{
对于($j=1;$j$triangle[$i+1][$j+1])
{
$clone_数组[$i][$j]+=$clone_数组[$i+1][$j];
}
其他的
{
$clone_数组[$i][$j]+=$clone_数组[$i+1][$j+1];
}
} 
} 
$pos=1;
$position=1;
$pathway=array();
$path[]=数组($triangle[1][1]);
对于($x=1;$x<$m;$x++)
{   
对于($position=$pos;$position<$pos+1;$position++)
{
如果($clone_数组[$x+1][$position]>$clone_数组[$x+1][$position+1])
{
$pathways[]=数组($triangle[$x+1][$position]);
$pos=$position;
}
其他的
{
$PATH[]=数组($x+1][$position+1]);
$pos=$position+1;
打破
}
}
}
返回路径;
}我是这样做的:)

函数maxPathSum($triangle,$m)
{ 
$clone_数组=$triangle;
对于($i=$m-1;$i>0;$i--)
{
对于($j=1;$j$triangle[$i+1][$j+1])
{
$clone_数组[$i][$j]+=$clone_数组[$i+1][$j];
}
其他的
{
$clone_数组[$i][$j]+=$clone_数组[$i+1][$j+1];
}
} 
} 
$pos=1;
$position=1;
$pathway=array();
$path[]=数组($triangle[1][1]);
对于($x=1;$x<$m;$x++)
{   
对于($position=$pos;$position<$pos+1;$position++)
{
如果($clone_数组[$x+1][$position]>$clone_数组[$x+1][$position+1])
{
$pathways[]=数组($triangle[$x+1][$position]);
$pos=$position;
}
其他的
{
$PATH[]=数组($x+1][$position+1]);
$pos=$position+1;
打破
}
}
}
返回路径;

}

那么,这段代码应该做什么?任何样本输入和输出?它需要给出最长路径的位置x=行y=列实际上它给了我最大路径的最大值但我需要注册一个跟踪你能给出任何样本输入和输出吗?输入3输出数组(3){[1]=>数组(1){[1]=>int(8)}[2]=>array(2){[1]=>int(5)[2]=>int(9)}[3]=>array(3){[1] =>int(9)[2]=>int(5)[3]=>int(6)}数组(3)[[0]=>array(2){[“x”]=>int(3)[“y”]=>int(3)}[1]=>array(2){[“x”]=>int(2)}[2]=>array(2){[“x”]=>int 1][“y”=>int 1}int 23)你能不能用适当的对齐方式将其添加到你的文章中?好吧,这段代码应该做什么?任何示例输入和输出?它需要给出最长路径的位置x=行y=列实际上它给了我最大路径的最大值,但我需要注册跟踪。你能给出任何示例输入和输出吗?输入3.输出阵列3.3.输出阵列3.3.3.3.输出阵列3.3.3.3.3.输出阵列3.3.3.3.3.3.3.输出阵列3.3.3.输出阵列3.3.3.3.3.3.3.3.3.3.3.3.3..3.1.3..1.1.3.3.1.3.3.3.3.3.3.3.3.3.3..3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3..3.3.3.3.3.3..3.3.....]=>int(1)}}int(23)你能不能把它添加到你的文章中,用适当的对齐方式编辑?
{ [1]=> array(1) { [1]=> int(8) } [2]=> array(2) { [1]=> int(5) [2]=> int(9) } [3]=> array(3) { [1]=> int(9) [2]=> int(5) [3]=> int(6) } } array(3) { [0]=> array(2) { ["x"]=> int(3) ["y"]=> int(3) } [1]=> array(2) { ["x"]=> int(2) ["y"]=> int(2) } [2]=> array(2) { ["x"]=> int(1) ["y"]=> int(1) } } int(23)
function maxPathSum($triangle, $m) 
{ 
    $clone_array=$triangle;
    for ( $i = $m - 1; $i > 0; $i--) 
    {
        for ($j = 1; $j <= $i; $j++) 
        { 
            if ($triangle[$i + 1][$j] > $triangle[$i + 1][$j + 1]) 
            {
            $clone_array[$i][$j] += $clone_array[$i + 1][$j];
            }
            else
            {
                $clone_array[$i][$j] += $clone_array[$i + 1][$j + 1];
            }
       } 
   } 
$pos=1;
$position=1;
$pathway=array();
$pathway[]= array($triangle[1][1]) ;
for( $x = 1 ; $x < $m ; $x++ )
{   
    for($position = $pos ; $position < $pos + 1 ; $position++)
    {
        if($clone_array[$x+1][$position] > $clone_array[$x+1][$position+1])
        {
            $pathway[]=array($triangle[$x+1][$position]);
            $pos=$position;
        }
        else
        {
            $pathway[]=array($triangle[$x+1][$position+1]);
            $pos = $position + 1 ;
            break;
        }
    }
}
return $pathway;