-
Notifications
You must be signed in to change notification settings - Fork 4
/
Lv2_빛의경로사이클.cpp
74 lines (61 loc) · 1.45 KB
/
Lv2_빛의경로사이클.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int n,m;
vector<int> answer;
bool check[500][500][4];
char arr[500][500];
int dx[4] = { 0,1,0,-1 }; // 위 오른쪽 아래 왼쪽
int dy[4] = { -1,0,1,0 };
void dfs(int y, int x, int dir, int cnt) {
if (check[y][x][dir]) {
answer.push_back(cnt - 1);
return;
}
check[y][x][dir] = true;
char c = arr[y][x];
int ny, nx;
int nd = dir;
if (c == 'L')
nd = (dir + 3) % 4;
else if (c == 'R')
nd = (dir + 1) % 4;
ny = y + dy[nd];
nx = x + dx[nd];
if (ny < 0)
ny += n;
else if (ny >= n)
ny -= n;
if (nx < 0)
nx += m;
else if (nx >= m)
nx -= m;
dfs(ny, nx, nd, cnt + 1);
}
vector<int> solution(vector<string> grid) {
n = grid.size();
m = grid[0].size();
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
arr[i][j] = grid[i][j];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
for (int k = 0; k < 4; k++) {
if (check[i][j][k]) // 갔던 곳은 pass
continue;
dfs(i, j, k, 1);
}
}
}
sort(answer.begin(), answer.end());
return answer;
}
int main() {
vector<string> grid = { "SL", "LR" };
vector<int> ans = solution(grid);
for (auto a : ans)
cout << a << " ";
return 0;
}