美文网首页
蜗牛算法

蜗牛算法

作者: MrBryan | 来源:发表于2020-06-15 15:06 被阅读0次
image.png

1 2 3 4 5 6 7 8 9
32 33 34 35 36 37 38 39 10
31 56 57 58 59 60 61 40 11
30 55 72 73 74 75 62 41 12
29 54 71 80 81 76 63 42 13
28 53 70 79 78 77 64 43 14
27 52 69 68 67 66 65 44 15
26 51 50 49 48 47 46 45 16
25 24 23 22 21 20 19 18 17

$obj = new SpinningSnail();
$obj->run();

class SpinningSnail
{

     public function run(){
        $total = 9;
        $result = array();
        $x = $y =  1;
        for ( $value = 1 ; $value <= ( $total * $total ) ;){
            $this->recursiveProcessing($result, $x, $y, $value, $total);
        }

        for ($x= 1 ; $x<= $total ; $x ++ ){
            for ( $y =1 ; $y <=$total;$y++ ){
                echo $result[ $x ][ $y ] . "\t";
            }
            echo "\n";
        }
        echo "beautiful ~  ! \n";
    }


    public function recursiveProcessing ( &$result , &$x , &$y,&$value, &$total){
        //先横1
        $this->crosswise( $result , $x ,  $y , $value , 1 , $total  );
        //在竖
        $y --;
        $x ++;
        $this->portait( $result , $x ,  $y , $value , 1 , $total  );
        $y --;
        $x --;
        $this->crosswise( $result , $x ,  $y , $value , 2 , $total  );
        $y ++;
        $x --;
        $this->portait( $result , $x ,  $y , $value , 2 , $total  );
        $x ++;
        $y ++;
        return $result;
    }

    public function  crosswise( &$result , &$x , &$y , &$value ,  $way = 1 , $total ){
        $flag = true;

        while ( $flag ){
            if ( empty( $result[$x][ $y ] ) && ( $y <= $total )  && ( $y >=  1 ) ){
                $result[ $x ][ $y ] = $value;
                $value ++;
                $y =( ( $way === 1 ) ? ($y + 1) : ( $y - 1 ) );

            }else{
                $flag = false;
            }
        }

        return true;
    }

    public function portait( &$result , &$x , &$y , &$value , $way = 1 , $total ){
        $flag = true;
        while ( $flag ){

            if ( empty( $result[$x][ $y ] )  && (  $x <=  $total ) && ( $x >= 1) ){
                $result[$x][ $y ] = $value;
                $value ++;
                $x =  ( ( $way === 1 ) ? ($x+1) : ($x-1) );
            }else{
                $flag = false;
            }
        }

        return true;
    }
}

golang-版本-还有优化空间

package arithmetic_test

import (
    "strconv"
    "testing"
)

func TestSnailArithmetic(t *testing.T) {
    number := 10
    t.Logf("输出 %[1]d*%[1]d 的矩阵\n", number)

    snail := snail(number)

    //输出
    for _, rows := range snail {
        s1 := ""
        for _, col := range rows {
            s1 = s1 + "\t" + strconv.Itoa(col)
        }
        t.Log(s1)
    }
}

func snail(number int) [][]int {

    var direction = [4][2]int{
        {0, 1},  // 右
        {1, 0},  // 下
        {0, -1}, // 左
        {-1, 0}, // 上
    }
    //初始化数据
    snail := make([][]int, number)
    for i := 0; i < number; i++ {
        snail[i] = make([]int, number)
    }

    tag := 1
    x := 0
    y := 0
    //循环处理数据
    for {
        //循环一圈的逻辑
        for i := 0; i < 4; i++ {
            snail, tag, x, y = pointerMovement(snail, direction[i], number, tag, x, y)
        }
        if tag > number*number {
            break
        }
    }
    return snail
}

func pointerMovement(snail [][]int, direction [2]int, number, tag int, x int, y int) ([][]int, int, int, int) {
    for {
        if tag > number*number {
            break
        }

        if snail[x][y] <= 0 {
            snail[x][y] = tag
            tag++
            if x+direction[0] < 0 || y+direction[1] < 0 || x+direction[0] >= number || y+direction[1] >= number || snail[x+direction[0]][y+direction[1]] > 0 {
                break
            } else {
                x = x + direction[0]
                y = y + direction[1]
            }
        } else {
            x = x + direction[0]
            y = y + direction[1]
        }
    }

    return snail, tag, x, y
}

相关文章

  • 蜗牛算法

    1 2 3 4 5 6 7 8 932 33 34 35 36 37 ...

  • 西米&西米妈成长日记Day126-听讲投资课

    书香陪伴 静待花开 Day126,今天爸爸陪西米阅读《蜗牛的日记》:蜗牛笑了;蜗牛哭了;蜗牛壳是红色的;蜗牛爸爸、...

  • 蜗牛蜗牛

    Never, never put yourself in a desperate situation. There...

  • 蜗牛 蜗牛

    蜗牛 蜗牛你有两只小小的角,在忙碌的生活里不停地寻找!蜗牛 蜗牛你有一双傻傻的眼睛,眨巴 眨巴找一个梦想会...

  • 蜗牛 蜗牛

    如果我是一只蜗牛 我要慢慢的在地球上漫步 我要轻嗅四季的芬芳 我要努力追上阳光的步伐 我要沐浴在大自然中 我要在夜...

  • 币名外号趣闻录(五)蜗牛和雪

    摘要:蜗牛币(RDD,Reddcoin)之所以外号蜗牛,是因为它的logo像一只蜗牛,是一只抽象化的蜗牛。蜗牛币采...

  • 蜗牛下载和设置

    vsco国内无法登陆,显示如下↓,所以需要蜗牛蜗牛蜗牛。。。。。【所有店都需要,滤镜下好后可以删掉的!】 蜗牛下载...

  • 《蜗牛》

    你说蜗牛是房奴 我说房奴是蜗牛 房奴蜗牛一样累 背着房贷汗水流 你说房奴是蜗牛 我说蜗牛是房奴 蜗牛房奴一样苦 背...

  • 蜗牛解忧小铺 等你来

    蜗牛解忧小铺今天开张了。我是小铺的主人蜗牛。蜗牛的蜗,蜗牛的牛。是不是很好记?一个致力于不断成长的蜗牛,一...

  • 蜗牛妈妈和小蜗牛

    蜗牛妈妈和小蜗牛 (来源:德阳晚报副刊玩主部落聊天记录 小蜗牛:梅林飘雪 蜗牛妈妈:节节草 整理:轻描淡写) ...

网友评论

      本文标题:蜗牛算法

      本文链接:https://www.haomeiwen.com/subject/jrzcxktx.html