Skip to content

Commit 39bfa6d

Browse files
authored
[20251114] PGM / LV3 / 여행경로 / 강신지
1 parent 4234d97 commit 39bfa6d

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
```
2+
import java.util.*;
3+
4+
class Solution {
5+
private String[][] tickets;
6+
private boolean[] used;
7+
private List<String> answer;
8+
private int n;
9+
10+
public String[] solution(String[][] tickets) {
11+
this.tickets = tickets;
12+
this.n = tickets.length;
13+
this.used = new boolean[n];
14+
15+
Arrays.sort(this.tickets, (a, b) -> {
16+
if (!a[0].equals(b[0])) {
17+
return a[0].compareTo(b[0]);
18+
}
19+
return a[1].compareTo(b[1]);
20+
});
21+
22+
List<String> path = new ArrayList<>();
23+
path.add("ICN");
24+
25+
dfs("ICN", 0, path);
26+
27+
return answer.toArray(new String[0]);
28+
}
29+
30+
private boolean dfs(String current, int count, List<String> path) {
31+
if (count == n) {
32+
answer = new ArrayList<>(path);
33+
return true;
34+
}
35+
36+
for (int i = 0; i < n; i++) {
37+
if (!used[i] && tickets[i][0].equals(current)) {
38+
used[i] = true;
39+
path.add(tickets[i][1]);
40+
41+
if (dfs(tickets[i][1], count + 1, path)) {
42+
return true;
43+
}
44+
45+
path.remove(path.size() - 1);
46+
used[i] = false;
47+
}
48+
}
49+
50+
return false;
51+
}
52+
}
53+
```

0 commit comments

Comments
 (0)