-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLv.0-21.cpp
43 lines (36 loc) · 839 Bytes
/
Lv.0-21.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
#include <string>
#include <vector>
#include <iostream>
using namespace std;
// r, d, l, u
int dx[4] = { 0, 1, 0, -1 };
int dy[4] = { 1, 0, -1, 0 };
vector<vector<int>> solution(int n) {
vector<vector<int>> answer;
answer.assign(n, vector<int>(n, -1));
int tmp = n;
int num = 1, d = 0;
int i = 0, j = 0;
answer[0][0] = num;
for (int k = 1; k < n; k++) {
num++;
i = i + dx[d];
j = j + dy[d];
answer[i][j] = num;
}
tmp--;
d = (d + 1) % 4;
while (num < n * n) {
for (int k = 0; k < 2; k++) {
for (int l = 0; l < tmp; l++) {
i = i + dx[d];
j = j + dy[d];
num++;
answer[i][j] = num;
}
d = (d + 1) % 4;
}
tmp--;
}
return answer;
}