https://leetcode.com/problems/split-array-largest-sum/
class Solution {
public:
int d[2][4] = {{1, 0, -1, 0}, {0, 1, 0, -1}};
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> sol;
int m = matrix.size();
if (m == 0) {
return sol;
}
int n = matrix[0].size();
if (n == 0) {
return sol;
}
vector<vector<int>> isUsed(m, vector<int>(n, 0));
int x = -1;
int y = 0;
int dir = 0;
int count = m * n;
while (count > 0) {
int nx = x + d[0][dir];
int ny = y + d[1][dir];
if (nx < 0 || nx >= n || ny < 0 || ny >= m || isUsed[ny][nx] == 1) {
dir = (dir + 1) % 4;
continue;
}
count--;
x = nx;
y = ny;
isUsed[y][x] = 1;
sol.push_back(matrix[y][x]);
}
return sol;
}
};