螺旋矩阵算法

探索螺旋矩阵的生成算法

最近看到博客里有人介绍螺旋矩阵,忍不住啊~

以下面的4*4螺旋矩阵为例

1  2  3    4

12  13   14  5

11  16   15  6

10  9   8   7

 

我们可以理解为一个二维数组,从四个方向(从左至右,从上至下,从右至左,从下至上)依次递增赋值(超出数组边界或遇到已赋值的数组元素则改变赋值方向,直至完成全部数组元素的赋值)。

 

初始时,赋值方向为从左至右:

    /*
     * 0 turn right
     * 1 turn down
     * 2 turn left
     * 3 turn up
     */
    int dir = 0;

 

改变赋值方向:

dir = (dir + 1)%4;

 

全部代码如下:

///////////////////////////////////////////////////////////
// Copyright (c) 2013, ShangHai xxxx Inc.
//
// FileName: spiralmatrix-2.cpp
//
// Description:
//
// Created: 2013年10月13日 星期日 19时42分47秒
// Revision: Revision: 1.0
// Compiler: g++
//
///////////////////////////////////////////////////////////
#include <stdio.h>
#include <string.h>

const int MAX_SIZE = 10;

int main()
{
    int array[MAX_SIZE][MAX_SIZE];
    memset(array, 0, sizeof(array));

    int x = 0;
    int y = 0;
    int totalStep = MAX_SIZE*MAX_SIZE;

    /*
     * 0 turn right
     * 1 turn down
     * 2 turn left
     * 3 turn up
     */
    int dir = 0;

    int count = 1;
    while(1)
    {
        array[x][y] = count;

        if(0 == dir)
        {
            if( ( (y+1) == MAX_SIZE) || (array[x][y+1] != 0) )
            {
                dir = (dir+1)%4;
                x = x + 1;
            }
            else
            {
                y = y + 1;
            }

        }
        else if(1 == dir)
        {
            if( ( (x+1) == MAX_SIZE) || (array[x+1][y] != 0) )
            {
                dir = (dir + 1)%4;
                y = y -1;
            }
            else
            {
                x = x + 1;
            }
        }
        else if(2 == dir)
        {
            if( ( (y-1) < 0) || (array[x][y-1] != 0) )
            {
                dir = (dir + 1)%4;
                x = x - 1;
            }
            else
            {
                y = y - 1;
            }
        }
        else if(3 == dir)
        {
            if( ( (x-1) < 0) || (array[x-1][y] != 0) )
            {
                dir = (dir + 1)%4;
                y = y + 1;
            }
            else
            {
                x = x - 1;
            }
        }

        //break or add moveStep
        if(count == totalStep)
            break;
        else
            count++;
    }

    //print
    for(int i = 0; i < MAX_SIZE; i++)
    {
        for(int j = 0; j < MAX_SIZE; j++)
            printf("%3d", array[i][j]);
        printf("\n");
    }

    return 0;
}

结果如下:

./spiralmatrix-2 
  1  2  3  4  5  6  7  8  9 10
 36 37 38 39 40 41 42 43 44 11
 35 64 65 66 67 68 69 70 45 12
 34 63 84 85 86 87 88 71 46 13
 33 62 83 96 97 98 89 72 47 14
 32 61 82 95100 99 90 73 48 15
 31 60 81 94 93 92 91 74 49 16
 30 59 80 79 78 77 76 75 50 17
 29 58 57 56 55 54 53 52 51 18
 28 27 26 25 24 23 22 21 20 19

 

 

 

 

 

 

转载于:https://www.cnblogs.com/yangtze736-2013-3-6/p/3367197.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值