Skip to content

Commit 48bfc96

Browse files
committed
Programmers_43164 : 여행경로
1 parent 839a8b4 commit 48bfc96

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
private String[][] tickets;
5+
private boolean[] visited;
6+
private List<List<String>> pathList;
7+
8+
public String[] solution(String[][] _tickets) {
9+
tickets = _tickets;
10+
visited = new boolean[tickets.length];
11+
pathList = new ArrayList<>();
12+
List<String> path = new ArrayList<>();
13+
path.add("ICN");
14+
path = dfs(path, "ICN");
15+
Collections.sort(pathList, (o1, o2) -> {
16+
for(int i=0; i<o1.size(); i++) {
17+
String s1 = o1.get(i);
18+
String s2 = o2.get(i);
19+
if(!s1.equals(s2)) {
20+
return s1.compareTo(s2);
21+
}
22+
}
23+
return 0;
24+
});
25+
String[] answer = pathList.get(0).stream().toArray(String[]::new);
26+
return answer;
27+
}
28+
29+
public List<String> dfs(List<String> path, String now) {
30+
if(path.size() == tickets.length+1) {
31+
pathList.add(new ArrayList<String>(path));
32+
return path;
33+
}
34+
35+
for(int i=0; i<tickets.length; i++) {
36+
String v1 = tickets[i][0];
37+
String v2 = tickets[i][1];
38+
if(v1.equals(now) && !visited[i]) {
39+
path.add(v2);
40+
visited[i] = true;
41+
dfs(path, v2);
42+
visited[i] = false;
43+
path.remove(path.size()-1);
44+
}
45+
}
46+
return path;
47+
}
48+
}

0 commit comments

Comments
 (0)