/**
* Note: The returned array must be malloced, assume caller calls free().
*/
//冲刺006:螺旋矩阵
int* spiralOrder(int** matrix, int matrixSize, int* matrixColSize, int* returnSize){
int row = matrixSize;
int col = matrixColSize[0];
if(row==0||col==0) return NULL;
int visit[row][col];
memset(visit, 0, sizeof(visit));
*returnSize = row * col;
int *spiralorder = (int*)malloc(sizeof(int)*(*returnSize));
int directions[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
// directions 数组:
//0行: --> 行数不变,列数加1
//1行: 下 行数加1,列数不变
//2行: <-- 行数不变,列数减1
//3行: 上 行数减1,列数不变
int index_directions=0;
int row_curr=0;
int col_curr=0;
int row_next=0;
int col_next=0;
for(int i=0;i<*returnSize;i++){
spiralorder[i] = matrix[row_curr][col_curr];
visit[row_curr][col_curr] = 1;
//计算下一步的行标列标
row_next = row_curr + directions[index_directions][0];
col_next = col_curr + directions[index_directions][1];
//判断下一步是否可行,不可行则更新index_directions
if(row_next>=row||row_next<0||col_next>=col||col_next<0||visit[row_next][col_next]!=0)
index_directions = (index_directions + 1) % 4;
row_curr += directions[index_directions][0];
col_curr += directions[index_directions][1];
}
return spiralorder;
}