Skip to content

Commit e0f05cb

Browse files
authored
[땃쥐] PROGRAMMERS(86052): 빛의 경로 사이클 - 문제풀이 추가 (#166)
1 parent db60cc4 commit e0f05cb

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

src/땃쥐/Programmers86052.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package 땃쥐;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class Programmers86052 {
8+
9+
private static final int COUNT_OF_DIRECTION = 4; // 동, 남, 서, 북
10+
private static final int[] DIRECTION_X = {1, 0, -1, 0}; // 동 남 서 북
11+
private static final int[] DIRECTION_Y = {0, 1, 0, -1}; // 동 남 서 북
12+
13+
private static int WIDTH;
14+
private static int HEIGHT;
15+
16+
private static boolean[][][] isVisited; // 각 칸별로 상하 좌우 방향으로 빛이 사출된 이력
17+
18+
public int[] solution(String[] grid) {
19+
20+
List<Integer> answer = new ArrayList<>();
21+
HEIGHT = grid.length;
22+
WIDTH = grid[0].length();
23+
24+
isVisited = new boolean[HEIGHT][WIDTH][COUNT_OF_DIRECTION];
25+
26+
for (int row = 0; row < HEIGHT; row++) {
27+
for (int column = 0; column < WIDTH; column++) {
28+
for (int d = 0; d < COUNT_OF_DIRECTION; d++) {
29+
if (!isVisited[row][column][d]) {
30+
answer.add(light(grid, row, column, d));
31+
}
32+
}
33+
}
34+
}
35+
return answer.stream()
36+
.sorted()
37+
.mapToInt(Integer::intValue)
38+
.toArray();
39+
}
40+
41+
private int light(String[] grid, int row, int column, int d) {
42+
int count = 0;
43+
44+
while (true) {
45+
if (isVisited[row][column][d]) { // 해당 방향으로 빛이 사출된 이력이 있는 경우 계산하지 않는다.
46+
break;
47+
}
48+
count++; // 거리 1 증가
49+
isVisited[row][column][d] = true; // 방문 처리
50+
if (grid[row].charAt(column) == 'L') { // 화살표를 반시계로 회전해야함. d를 -1만큼 움직여야함.
51+
d = (d == 0) ? 3 : d - 1; // 반시계방향 (좌회전)
52+
} else if (grid[row].charAt(column) == 'R') { // 화살표를 시계방향으로 회전해야함. d를 +1만큼 움직여야함.
53+
d = (d == 3) ? 0 : d + 1; // 시계방향 (우회전)
54+
}
55+
56+
row = (row + DIRECTION_Y[d] + HEIGHT) % HEIGHT; // 방향을 더했을 때 음이 될 수 있어서 최대 높이/너비 값을 한번 더하고 나머지 처리 해야함
57+
column = (column + DIRECTION_X[d] + WIDTH) % WIDTH;
58+
}
59+
return count;
60+
}
61+
62+
}

0 commit comments

Comments
 (0)