Skip to content

Commit c557a2c

Browse files
authored
Create 프로그래머스_방문길이.java
1 parent 9239821 commit c557a2c

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import java.util.*;
2+
class Solution {
3+
public int solution(String dirs) {
4+
int answer = 0;
5+
6+
int [][] array = new int[11][11];
7+
Set<String> set = new HashSet<>();
8+
9+
int [] dx = {1,-1};
10+
int [] dy = {1,-1};
11+
int cx = 0;
12+
int cy = 0;
13+
14+
for (int i = 0;i < dirs.length() ; i++) {
15+
char c = dirs.charAt(i);
16+
int n;
17+
if (c == 'U') {
18+
n = cy + dy[0];
19+
if (n <= 5 && n >= -5) {
20+
21+
set.add(cx + " " + cy + "," + cx + " " + n);
22+
set.add(cx + " " + n + "," + cx + " " + cy);
23+
cy = n;
24+
}
25+
} else if (c == 'D') {
26+
n = cy + dy[1];
27+
if (n <= 5 && n >= -5) {
28+
set.add(cx + " " + cy + "," + cx + " " + n);
29+
set.add(cx + " " + n + "," + cx + " " + cy);
30+
cy = n;
31+
}
32+
} else if (c == 'R') {
33+
n = cx + dx[0];
34+
if (n <= 5 && n >= -5) {
35+
set.add(cx + " " + cy + "," + n + " " + cy);
36+
set.add(n + " " + cy + "," + cx + " " + cy);
37+
cx = n;
38+
}
39+
} else {
40+
n = cx + dx[1];
41+
if (n <= 5 && n >= -5) {
42+
set.add(cx + " " + cy + "," + n + " " + cy);
43+
set.add(n + " " + cy + "," + cx + " " + cy);
44+
cx = n;
45+
}
46+
}
47+
48+
}
49+
50+
51+
return set.size() / 2;
52+
}
53+
54+
55+
}

0 commit comments

Comments
 (0)