题目链接
蛇形矩阵的思路
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
int m = matrix.size(), n = matrix[0].size();
int q[m][n];
memset(q,0,sizeof(q));
int x = 0, y = 0, d = 1, k = 0, dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
vector<int> arr(m * n);
for(int i = 0; i < m * n; i++){
q[x][y] = 1;
arr[k++] = matrix[x][y];
int a = x + dx[d], b = y + dy[d];
if(a < 0 || a >= m || b < 0 || b >= n || q[a][b]){
d = (d + 1) % 4;
a = x + dx[d], b = y + dy[d];
}
x = a, y = b;
}
return arr;
}
};