-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHW_86052.java
More file actions
50 lines (46 loc) ยท 1.55 KB
/
HW_86052.java
File metadata and controls
50 lines (46 loc) ยท 1.55 KB
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
// ์ฃผ์ด์ง ๊ฒฉ์๋ฅผ ํตํด ๋ง๋ค์ด์ง๋ ๋น์ ๊ฒฝ๋ก ์ฌ์ดํด์ ๋ชจ๋ ๊ธธ์ด๋ค์ ๋ฐฐ์ด์ ๋ด์ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ
// ์๊ฐ๋ณต์ก๋ : 500 * 500 * 4 -> ๊ฐ๋ฅ -> O(NM)
import java.util.*;
class HW_86052 {
static int[] dx = {1, 0, -1, 0};
static int[] dy = {0, 1, 0, -1};
static int r, c;
static boolean[][][] visited;
public int[] solution(String[] grid) {
r = grid.length;
c = grid[0].length();
visited = new boolean[r][c][4];
List<Integer> answer = new ArrayList<>();
for(int i=0; i<r; i++){
for(int j=0; j<c; j++){
for(int d=0; d<4; d++){
if(!visited[i][j][d]){
answer.add(rotate(grid, i, j, d));
}
}
}
}
Collections.sort(answer); // ์ค๋ฆ์ฐจ์ ์ ๋ ฌ
int[] result = new int[answer.size()]; // ๋ฐฐ์ด๋ก ๋ณํ ํ ์ถ๋ ฅ
for (int i = 0; i < answer.size(); i++) {
result[i] = answer.get(i);
}
return result;
}
static int rotate(String[] grid, int x, int y, int dir){
int cnt = 0;
while(!visited[x][y][dir]){
visited[x][y][dir] = true;
cnt++;
String input = String.valueOf(grid[x].charAt(y));
if(input.equals("L")){
dir = (dir+3)%4;
} else if(input.equals("R")){
dir = (dir+1)%4;
}
x = (x+dx[dir]+r) % r;
y = (y+dy[dir]+c) % c;
}
return cnt;
}
}