-
Notifications
You must be signed in to change notification settings - Fork 0
/
200106-1.cpp
55 lines (52 loc) · 1.12 KB
/
200106-1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// https://leetcode-cn.com/problems/spiral-matrix-ii/
#include <cstdio>
#include <vector>
using namespace std;
void print(const vector<vector<int>>& a)
{
printf("[\n");
for (const auto& r : a) {
printf(" [");
for (size_t i = 0; i < r.size(); ++i) {
printf(" %d%s", r[i], (i + 1 < r.size() ? "," : " ]\n"));
}
}
printf("]\n");
}
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> a(n);
for (int i = 0; i < n; ++i) {
a[i].resize(n);
}
for (int x = 0, y = 0, dx = 1, dy = 0, c = 1; c <= n * n; ++c) {
a[y][x] = c;
int x2 = x + dx;
int y2 = y + dy;
if (x2 >= 0 && x2 < n && y2 >= 0 && y2 < n && a[y2][x2] == 0) {
x = x2; y = y2; continue;
}
if (dx == 1 && dy == 0) {
dx = 0; dy = 1;
} else if (dx == 0 && dy == 1) {
dx = -1; dy = 0;
} else if (dx == -1 && dy == 0) {
dx = 0; dy = -1;
} else if (dx == 0 && dy == -1) {
dx = 1; dy = 0;
}
x += dx; y += dy;
}
return a;
}
};
int main()
{
Solution s;
print(s.generateMatrix(1));
print(s.generateMatrix(2));
print(s.generateMatrix(3));
print(s.generateMatrix(4));
return 0;
}