-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDH_49994.java
More file actions
49 lines (41 loc) ยท 1.13 KB
/
DH_49994.java
File metadata and controls
49 lines (41 loc) ยท 1.13 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
/*
๋ฐฉ๋ฌธ ๊ธธ์ด
*/
class DH_49994 {
static class Point {
int r, c;
public Point(int r, int c) {
this.r = r;
this.c = c;
}
}
static int dr[] = {-1, 0, 1, 0};
static int dc[] = {0, -1, 0, 1};
static boolean check(int r, int c) {
return r >= 0 && r < 11 && c >= 0 && c < 11;
}
public int solution(String dirs) {
boolean v[][][] = new boolean[4][11][11];
int answer = 0;
Point p = new Point(5, 5);
for(int i = 0; i < dirs.length(); i++) {
char ch = dirs.charAt(i);
int dir = 0;
if(ch == 'U') dir = 0;
if(ch == 'L') dir = 1;
if(ch == 'D') dir = 2;
if(ch == 'R') dir = 3;
int nr = p.r + dr[dir];
int nc = p.c + dc[dir];
if(!check(nr, nc)) continue;
if(!v[dir][p.r][p.c] && !v[(dir + 2) % 4][nr][nc]) {
v[dir][p.r][p.c] = true;
v[(dir + 2) % 4][nr][nc] = true;
answer++;
}
p.r = nr;
p.c = nc;
}
return answer;
}
}