如何打印螺旋阵列PHP

时间:2014-06-20 07:48:37

标签: php

我有数组和这个功能

$ array = array(array(00,01,02,03),array(10,11,12,13),array(20,21,22,23));

function spiral($xSize, $ySize, array $matrix) {
$k = 0;
$l = 0;
$xSize--;
$ySize--;

while ($k <= $xSize && $l <= $ySize) {
    for ($i = $l; $i <= $ySize; ++$i) {
        print($matrix[$k][$i] + " ");
    }
    $k++;

    for ($i = $k; $i <= $xSize; ++$i) {
        print($matrix[$i][$ySize] + " ");
    }
    $ySize--;

    for ($i = $ySize; $i >= $l; --$i) {
        print($matrix[$xSize][$i] + " ");
    }
    $xSize--;

    for ($i = $xSize; $i >= $k; --$i) {
        print($matrix[$i][$l] + " ");
    }
    $l++;
}
}

但不工作。功能必须返回此 00 01 02 03 13 23 22 21 20 10 11 12

但要归还 01 23 13 23 22 21 20 10 11 12 11

3 个答案:

答案 0 :(得分:0)

你的职能:

function spiral($xSize, $ySize, array $matrix) {
    $m = 0;
    $n = 0;
    $xSize--;
    $ySize--;

    while ($m <= $xSize && $n <= $ySize) {
        for ($i = $n; $i <= $ySize; ++$i) {
            print($matrix[$m][$i] . " ");
        }
        $m++;
        for ($i=$m; $i <= $xSize ; $i++) { 
            print($matrix[$i][$ySize] . " ");
        }
        $ySize--;
        for ($i=$ySize; $i >= $n ; $i--) { 
            print($matrix[$xSize][$i] . " ");
        }
        $xSize--;
        for ($i=$xSize; $i >= $m ; $i--) { 
            print($matrix[$i][$n] . " ");
        }
        $n++;
    }
}

答案 1 :(得分:0)

您可以使用此处讨论的代码的略微更改版本: getting a total count arrays of a multi-dimentional array

我的版本改变了如下:

Class CountArrays{
    public $elements = array();

    function count(array $array){
        foreach($array as $entry){
            if(is_array($entry){
                $this->count++;
                $this->count($entry);
            } else {
                $this->elements[] = $entry;
            }
        }
    }

}

答案 2 :(得分:0)

我有一个更好的解决方案。 这是打印螺旋阵列的方法:

function printSpiralArray($xSize, $ySize, $matrix){
    $printed = [];
    $numElems = $xSize*$ySize;
    $pos = [0,-1];
    $direction = 0;
    for($i=0;$i<$numElems;$i++){
        $checkPos = $pos;
        switch($direction%4){
            case 0:
                $checkPos[1]++;
            break;
            case 1:
                $checkPos[0]++;
            break;
            case 2:
                $checkPos[1]--;
            break;
            case 3:
                $checkPos[0]--;
            break;
        }
        if($checkPos[0]>=$xSize || $checkPos[1]>=$ySize || $checkPos[0]<0 || $checkPos[1]<0 || in_array($checkPos[0].'_'.$checkPos[1],$printed)){
            $direction++;
            $i--;
            continue;
        }
        $pos = $checkPos;
        echo $matrix[$pos[0]][$pos[1]]."\t";
        $printed[] = $pos[0].'_'.$pos[1];
    }
}

以下是构建给定大小的螺旋阵列的方法:

function getSpiralArray($size){
    $matrix = [];
    $numElems = $size*$size;
    $pos = [0,-1];
    $direction = 0;
    $num = 1;
    for($i=0;$i<$numElems;$i++){
        $checkPos = $pos;
        switch($direction%4){
            case 0:
                $checkPos[1]++;
            break;
            case 1:
                $checkPos[0]++;
            break;
            case 2:
                $checkPos[1]--;
            break;
            case 3:
                $checkPos[0]--;
            break;
        }
        if($checkPos[0]>=$size || $checkPos[1]>=$size || $checkPos[0]<0 || $checkPos[1]<0 || isset($matrix[$checkPos[0]][$checkPos[1]])){
            $direction++;
            $i--;
            continue;
        }
        $pos = $checkPos;
        $matrix[$pos[0]][$pos[1]] = $num;
        $num++;

    }
    return $matrix;
}