-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJW_49994.java
More file actions
46 lines (43 loc) · 1.88 KB
/
JW_49994.java
File metadata and controls
46 lines (43 loc) · 1.88 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
class JW_49994 {
boolean[][][] board = new boolean[11][11][4]; // 방향을 확인할 추가 공간
int[] dy = { 1, -1, 0, 0 }; // 상 하 우 좌
int[] dx = { 0, 0, 1, -1 };
char[] dirsArr; // 주어진 문자열을 캐릭터형 배열로 저장
int area; // 방문한 길이
public int solution(String dirs) {
dirsArr = dirs.toCharArray();
recursion(5, 5, 0); // 재귀 호출
return area;
}
// 캐릭터의 움직임을 구현할 재귀함수
private void recursion(int y, int x, int depth) {
if (depth == dirsArr.length) // 종료 조건
return;
int to = 0; // to : 현재 위치에서 들어가는 방향
int from = 1; // from : 다음 위치에서 들어오는 방향
if (dirsArr[depth] == 'D') {
to = 1;
from = 0;
} else if (dirsArr[depth] == 'R') {
to = 2;
from = 3;
} else if (dirsArr[depth] == 'L') {
to = 3;
from = 2;
}
int ny = y + dy[to], nx = x + dx[to];
if (isValid(ny, nx)) { // 다음 위치 경계 체크
// 다음 지점의 들어오는 방향의 방문 체크가 안되어있다면
if (!board[ny][nx][from])
area++; // 방문 길이 증가
board[y][x][to] = true; // 현재 방향 방문 체크
board[ny][nx][from] = true; // 다음 방향 방문 체크
recursion(ny, nx, depth + 1); // 다음 재귀 함수 호출
} else
recursion(y, x, depth + 1); // 경계를 넘으면 무시하고 다음 재귀 함수 호출
}
// 경계 조건
private boolean isValid(int y, int x) {
return 0 <= y && y < 11 && 0 <= x && x < 11;
}
}