Skip to content

Commit 94d9bd4

Browse files
authored
Merge pull request #1468 from AlgorithmWithGod/khj20006
[20251121] PGM / LV2 / 삼각 달팽이 / 권혁준
2 parents b7e106e + e5d64fa commit 94d9bd4

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
```cpp
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
const int dx[3] = {1,0,-1};
6+
const int dy[3] = {0,1,-1};
7+
8+
int N;
9+
int arr[1000][1000]{};
10+
bool vis[1000][1000]{};
11+
12+
bool oob(int x, int y) {
13+
return x<0 || x>=N || y<0 || y>x;
14+
}
15+
16+
void go(int x, int y, int value, int dir) {
17+
if(value > N*(N+1)/2) return;
18+
arr[x][y] = value;
19+
vis[x][y] = true;
20+
int xx = x + dx[dir];
21+
int yy = y + dy[dir];
22+
if(oob(xx,yy) || vis[xx][yy]) {
23+
dir = (dir + 1) % 3;
24+
xx = x + dx[dir];
25+
yy = y + dy[dir];
26+
}
27+
go(xx,yy,value+1,dir);
28+
}
29+
30+
vector<int> solution(int n) {
31+
N = n;
32+
go(0,0,1,0);
33+
vector<int> answer;
34+
for(int i=0;i<N;i++) for(int j=0;j<=i;j++) answer.push_back(arr[i][j]);
35+
return answer;
36+
}
37+
```

0 commit comments

Comments
 (0)