leetcode 885. Spiral Matrix III
题意:大概就是给你一个起点,在矩阵往四个方向绕,把点按循序保存就好了
思路:绕就完事了
class Solution {
public:
vector<vector<int>> spiralMatrixIII(int R, int C, int r0, int c0) {
vector<vector<int>> ans;
int x[4] = { 0, 1, 0, -1 };
int y[4] = { 1, 0, -1, 0 };
int dir = 0;
int step = 1;
int now_x = r0;
int now_y = c0;
ans.push_back(vector<int>{r0,c0});
//cout << "[" << now_x << "," << now_y << "]" << ',';
int num = 1;
while (num<R*C)
{
for (int i = 1; i <= step; i++)
{
now_x = now_x + x[dir];
now_y = now_y + y[dir];
if (now_x >= 0 && now_x < R &&now_y >= 0 && now_y < C)
{
ans.push_back(vector<int>{now_x, now_y});
//cout << "[" << now_x << "," << now_y << "]" << ',';
num++;
}
}
dir = (dir + 1) % 4;
for (int i = 1; i <= step; i++)
{
now_x = now_x + x[dir];
now_y = now_y + y[dir];
if (now_x >= 0 && now_x < R &&now_y >= 0 && now_y < C)
{
ans.push_back(vector<int>{now_x, now_y});
//cout << "[" << now_x << "," << now_y << "]" << ',';
num++;
}
}
dir = (dir + 1) % 4;
step += 1;
}
return ans;
}
};