Skip to content

Commit 939b33c

Browse files
committed
[level 2] Title: 빛의 경로 사이클, Time: 73.67 ms, Memory: 133 MB -BaekjoonHub
1 parent cdfa961 commit 939b33c

File tree

2 files changed

+39
-33
lines changed

2 files changed

+39
-33
lines changed

프로그래머스/2/86052. 빛의 경로 사이클/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# [level 2] 빛의 경로 사이클 - 86052
22

3-
[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/86052)
3+
[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/86052#qna)
44

55
### 성능 요약
66

7-
메모리: 106 MB, 시간: 64.65 ms
7+
메모리: 133 MB, 시간: 73.67 ms
88

99
### 구분
1010

@@ -16,7 +16,7 @@
1616

1717
### 제출 일자
1818

19-
2024년 11월 20일 05:55:28
19+
2024년 11월 22일 17:24:58
2020

2121
### 문제 설명
2222

프로그래머스/2/86052. 빛의 경로 사이클/빛의 경로 사이클.java

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,52 @@
1-
import java.util.ArrayList;
1+
import java.util.*;
22

33
class Solution {
4-
static int R, C;
5-
static int[] dr = { -1, 0, 1, 0 }, dc = { 0, -1, 0, 1 }; // 아래, 왼, 위, 오른
6-
static boolean[][][] isVisited;
4+
int m, n, idx;
5+
6+
//상우좌하
7+
int[] dx = { -1, 0, 1, 0 };
8+
int[] dy = { 0, 1, 0, -1 };
9+
boolean[][][] visited;
10+
int[] answer;
711

812
public int[] solution(String[] grid) {
9-
ArrayList<Integer> answer = new ArrayList<Integer>();
13+
m = grid.length;
14+
n = grid[0].length();
15+
answer = new int[n * m * 4];
1016

11-
R = grid.length;
12-
C = grid[0].length();
13-
14-
isVisited = new boolean[R][C][4];
15-
for (int i = 0; i < R; i++) {
16-
for (int j = 0; j < C; j++) {
17+
visited = new boolean[m][n][4];
18+
for (int i = 0; i < m; i++) {
19+
for (int j = 0; j < n; j++) {
1720
for (int d = 0; d < 4; d++) {
18-
if (!isVisited[i][j][d])
19-
answer.add(light(grid, i, j, d ));
21+
if (!visited[i][j][d]) answer[idx++] = go(grid, i, j, d );
2022
}
2123
}
2224
}
23-
24-
return answer.stream().sorted().mapToInt(i -> i).toArray();
25+
answer = Arrays.copyOfRange(answer, 0, idx);
26+
Arrays.sort(answer);
27+
return answer;
2528
}
2629

27-
private static int light(String[] grid, int r, int c, int d) {
28-
int cnt = 0; // 이동거리
30+
int go(String[] grid, int r, int c, int d) {
31+
int cnt = 0;
2932

3033
while (true) {
31-
if (isVisited[r][c][d])
32-
break;
33-
34-
cnt++; // 거리증가
35-
isVisited[r][c][d] = true; // 방문처리
36-
37-
if (grid[r].charAt(c) == 'L')
38-
d = d == 0 ? 3 : d - 1; // 좌회전
39-
else if (grid[r].charAt(c) == 'R')
40-
d = d == 3 ? 0 : d + 1; // 우회전
41-
42-
r = (r + dr[d] + R) % R;
43-
c = (c + dc[d] + C) % C;
34+
if (visited[r][c][d]) break;
35+
36+
cnt++;
37+
visited[r][c][d] = true;
38+
39+
if (grid[r].charAt(c) == 'L'){
40+
if(d == 0) d = 3;
41+
else d -= 1;
42+
43+
}else if (grid[r].charAt(c) == 'R'){
44+
if(d == 3) d = 0;
45+
else d += 1;
46+
}
47+
48+
r = (r + dx[d] + m) % m;
49+
c = (c + dy[d] + n) % n;
4450
}
4551

4652
return cnt;

0 commit comments

Comments
 (0)