php-函数中的递归二维数组

发布于 2024-09-18 14:55:18 字数 1351 浏览 1 评论 0原文

我有一个二维数组,数组 A[60][150],还有另一个数组,数组 B[60][150]。 现在我想做的是:

给定数组 A 中的一个点 x,yi 想要访问它的邻居来查找两个元素之间的相似性。如果它们相似,则找到其邻居。 所以现在我正在使用递归函数来做到这一点。但它会抛出一个错误,指出已超出最大执行时间或内存不足。

所以我只是想知道是否还有其他方法可以解决这个问题。我的意思是不使用递归。

代码示例:

           protected function find_neighbor($y,$x,$garray,$gr)
       {
        $r=$garray[$y][$x-1]['red'];
        if(true){
            $this->find_neighbor($y,$x-1,$garray,$gr);
        }

        $r=$garray[$y-1][$x-1]['red'];
        if(true){
            $this->find_neighbor($y-1,$x-1,$garray,$gr);
        }

        $r=$garray[$y-1][$x]['red'];
        if(true){
            $this->find_neighbor($y-1,$x,$garray,$gr);
        }

        $r=$garray[$y-1][$x+1]['red'];
        if(true){
            $this->find_neighbor($y-1,$x+1,$garray,$gr)
        }

        $r=$garray[$y][$x+1]['red'];
        if(true){
            $this->find_neighbor($y,$x+1,$garray,$gr);      
        }

        $r=$garray[$y+1][$x+1]['red'];
        if(true){
            $this->find_neighbor($y+1,$x+1,$garray,$gr);
        } 

        $r=$garray[$y+1][$x]['red'];
        if(true){
                $this->find_neighbor($y+1,$x,$garray,$gr);  
        } 

        $r=$garray[$y+1][$x-1]['red'];
        if(true){
            $this->find_neighbor($y+1,$x-1,$garray,$gr);
        }
}

I have a 2d array say, Array A[60][150] and have another array, Array B[60][150].
Now what I am trying to do is:

Given a point in Array A say x,y i want to access its neighbors to find similarity between the two elements. if they are similar then find its neighbors.
So right now i am using recursive function to do it. But its throws an error saying maximum execution time has exceeded or out of memory.

So I am just wondering is there any other to solve this. I mean without using recursion.

code example:

           protected function find_neighbor($y,$x,$garray,$gr)
       {
        $r=$garray[$y][$x-1]['red'];
        if(true){
            $this->find_neighbor($y,$x-1,$garray,$gr);
        }

        $r=$garray[$y-1][$x-1]['red'];
        if(true){
            $this->find_neighbor($y-1,$x-1,$garray,$gr);
        }

        $r=$garray[$y-1][$x]['red'];
        if(true){
            $this->find_neighbor($y-1,$x,$garray,$gr);
        }

        $r=$garray[$y-1][$x+1]['red'];
        if(true){
            $this->find_neighbor($y-1,$x+1,$garray,$gr)
        }

        $r=$garray[$y][$x+1]['red'];
        if(true){
            $this->find_neighbor($y,$x+1,$garray,$gr);      
        }

        $r=$garray[$y+1][$x+1]['red'];
        if(true){
            $this->find_neighbor($y+1,$x+1,$garray,$gr);
        } 

        $r=$garray[$y+1][$x]['red'];
        if(true){
                $this->find_neighbor($y+1,$x,$garray,$gr);  
        } 

        $r=$garray[$y+1][$x-1]['red'];
        if(true){
            $this->find_neighbor($y+1,$x-1,$garray,$gr);
        }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅冇旳回忆 2024-09-25 14:55:18

未经测试:

$range = 3;
foreach ($A as $A1=>$tmp) {
    foreach ($tmp as $A2=>$value) {
        for ($x=0-$range; $x<=$range; $x++) {
            for ($y=0-$range; $y<=$range; $y++) {
                $F1 = $A1+$x;
                $F2 = $A2+$y;
                if (isset($A[$F1][$F2])) {
                    print "A[{$A1}][{$A2}] is close to A[{$F1}][{$F2}]<br>";
                }
            }
        }
    }
}

Untested:

$range = 3;
foreach ($A as $A1=>$tmp) {
    foreach ($tmp as $A2=>$value) {
        for ($x=0-$range; $x<=$range; $x++) {
            for ($y=0-$range; $y<=$range; $y++) {
                $F1 = $A1+$x;
                $F2 = $A2+$y;
                if (isset($A[$F1][$F2])) {
                    print "A[{$A1}][{$A2}] is close to A[{$F1}][{$F2}]<br>";
                }
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文