Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c04a7fa

Browse files
committed
Lv2_빛의경로사이클
1 parent d3486a1 commit c04a7fa

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <string>
2+
#include <vector>
3+
#include <iostream>
4+
#include <algorithm>
5+
using namespace std;
6+
7+
int n,m;
8+
vector<int> answer;
9+
bool check[500][500][4];
10+
char arr[500][500];
11+
int dx[4] = { 0,1,0,-1 }; // 위 오른쪽 아래 왼쪽
12+
int dy[4] = { -1,0,1,0 };
13+
14+
void dfs(int y, int x, int dir, int cnt) {
15+
if (check[y][x][dir]) {
16+
answer.push_back(cnt - 1);
17+
return;
18+
}
19+
20+
check[y][x][dir] = true;
21+
char c = arr[y][x];
22+
23+
int ny, nx;
24+
int nd = dir;
25+
if (c == 'L')
26+
nd = (dir + 3) % 4;
27+
else if (c == 'R')
28+
nd = (dir + 1) % 4;
29+
30+
ny = y + dy[nd];
31+
nx = x + dx[nd];
32+
if (ny < 0)
33+
ny += n;
34+
else if (ny >= n)
35+
ny -= n;
36+
37+
if (nx < 0)
38+
nx += m;
39+
else if (nx >= m)
40+
nx -= m;
41+
42+
dfs(ny, nx, nd, cnt + 1);
43+
}
44+
45+
vector<int> solution(vector<string> grid) {
46+
47+
n = grid.size();
48+
m = grid[0].size();
49+
50+
for (int i = 0; i < n; i++)
51+
for (int j = 0; j < m; j++)
52+
arr[i][j] = grid[i][j];
53+
54+
for (int i = 0; i < n; i++) {
55+
for (int j = 0; j < m; j++) {
56+
for (int k = 0; k < 4; k++) {
57+
if (check[i][j][k]) // 갔던 곳은 pass
58+
continue;
59+
dfs(i, j, k, 1);
60+
}
61+
}
62+
}
63+
sort(answer.begin(), answer.end());
64+
65+
return answer;
66+
}
67+
68+
int main() {
69+
vector<string> grid = { "SL", "LR" };
70+
vector<int> ans = solution(grid);
71+
for (auto a : ans)
72+
cout << a << " ";
73+
return 0;
74+
}

‎Programmers/Programmers.vcxproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@
232232
<ClCompile Include="Lv2\Lv2_메뉴리뉴얼.cpp">
233233
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
234234
</ClCompile>
235+
<ClCompile Include="Lv2\Lv2_빛의경로사이클.cpp">
236+
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
237+
</ClCompile>
235238
<ClCompile Include="Lv2\Lv2_소수만들기.cpp">
236239
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
237240
</ClCompile>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /