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+ }
0 commit comments