Repository navigation
Expand file tree
/
Copy pathJW_49994.java
More file actions
46 lines (43 loc) ยท 1.88 KB
/
Copy pathJW_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;
}
}