螺旋矩阵-【无聊模拟】

#include <stdio.h>

#define Max_Size        256
#define INITIAL_CH        0
#define Value_Start        1

typedef enum {
    Dir_Center = 0,
    Dir_Left,
    Dir_Bottom,
    Dir_Right,
    Dir_Top,
    Dir_Max
};

int Map[Max_Size][Max_Size];

/*
0,0------------->iy
 |
 |
 |
 |
 ^ix
 iSize is the array size,
 iDirect is current Direct.

 he will return the next usefull direct.
*/
int reDirect(int *ix, int *iy, int iSize, int iDirect) {
    int reDirectValue = Dir_Center;    //Next Direct
    int iFullDirectChange = 1;            //Check the Full Direct
    while (iFullDirectChange && iFullDirectChange < Dir_Max) {
        switch (iDirect)
        {
        case Dir_Left:
            /* TODO : Current Direct is left, 
                    Check the Left is not out of array.
                    and Test the the map value is not used.
                    then still use the current direct, change the current index.
                    otherwise change the next direct. */
            if ((*iy - 1) >= 0 && Map[*ix][*iy - 1] == INITIAL_CH) {    //Left
                *iy -= 1;
                reDirectValue = Dir_Left;
                iFullDirectChange = 0;
            } else {
                reDirectValue = Dir_Bottom;
                iFullDirectChange++;
            }
            break;
        case Dir_Bottom:
            if ((*ix + 1) < iSize && Map[*ix + 1][*iy] == INITIAL_CH) {    // bottom
                *ix += 1;
                reDirectValue = Dir_Bottom;
                iFullDirectChange = 0;
            } else {
                reDirectValue = Dir_Right;
                iFullDirectChange++;
            }
            break;
        case Dir_Right:
            if ((*iy + 1) < iSize && Map[*ix][*iy + 1] == INITIAL_CH) {    //Right
                *iy += 1;
                reDirectValue = Dir_Right;
                iFullDirectChange = 0;
            } else {
                reDirectValue = Dir_Top;
                iFullDirectChange++;
            }
            break;
        case Dir_Top:
            if ((*ix - 1) >= 0 && Map[*ix - 1][*iy] == INITIAL_CH) {    //Top
                *ix -= 1;
                reDirectValue = Dir_Top;
                iFullDirectChange = 0;
            } else {
                reDirectValue = Dir_Left;
                iFullDirectChange++;
            }
            break;
        default: printf("Error!\n"); iFullDirectChange = 0; break;
        }

        iDirect = reDirectValue;
    }

    return iFullDirectChange == Dir_Max ? Dir_Center : reDirectValue;
}

/*
use this function to initial the array.
*/
void initial(int iValue, int iSize) {
    int ix = 0, iy = 0;
    int CurDirect = Dir_Left;
    for (ix = 0; ix < iSize; ix++) {
        for (iy = 0; iy < iSize; iy++) {
            Map[ix][iy] = INITIAL_CH;
        }
    }
    
    for ( ix = 0, iy = 0; Dir_Center != CurDirect; CurDirect = reDirect(&ix, &iy, iSize, CurDirect) ) {
        Map[ix][iy] = iValue++;
    }
}

/*
use this function to show array value.
*/
void DrawArray(int iSize) {
    int ix = 0, iy = 0;
    for (ix = 0; ix < iSize; ix++) {
        for (iy = 0; iy < iSize; iy++) {
            printf( "%d ", Map[ix][iy]);
        }
        printf( "\n");
    }
}

int main() {
    
    int iValue = Value_Start;
    int iSize = 4;

    initial(iValue, iSize);
    DrawArray(iSize);

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值