授惑
1 4
2 3
1 8 7
2 9 6
3 4 5
1 12 11 10
2 13 16 9
3 14 15 8
4 5 6 7
.....
怎样用数组构造这样的结构啊。大家帮帮忙呗,要是有注释就更好了,
最后祝大家新年快乐,学习顺利,
[解决办法]
baidu螺旋矩阵
[解决办法]
#include <stdio.h>#define Max_Size 256#define INITIAL_CH 0#define Value_Start 1typedef 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 = 3; initial(iValue, iSize); DrawArray(iSize); return 0;}
[解决办法]
#include <stdio.h>#define Max_Size 256#define INITIAL_CH 0#define Value_Start 1typedef 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;}