-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp43164.java
41 lines (33 loc) · 1.21 KB
/
p43164.java
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
package programmers;
import java.util.*;
public class P43164 {
private ArrayList<String> list;
private boolean[] used;
public String[] solution(String[][] tickets) {
String[] answer = {};
list = new ArrayList<>();
used = new boolean[tickets.length];
dfs(tickets, used, "ICN", "ICN", 0);
Collections.sort(list);
answer = list.get(0).split(" ");
return answer;
}
public void dfs(String[][] tickets, boolean[] used, String current, String plan, int count) {
if (count == tickets.length) {
list.add(plan);
return;
}
for (int i=0; i<tickets.length; i++) {
if (used[i] == false && tickets[i][0].equals(current)) {
used[i] = true;
dfs(tickets, used, tickets[i][1], plan + " " + tickets[i][1], count+1);
used[i] = false;
}
}
}
public static void main(String[] args) {
String[][] tickets = {{"ICN", "JFK"}, {"HND", "IAD"}, {"JFK", "HND"}};
// String[][] tickets = {{"ICN", "SFO"}, {"ICN", "ATL"}, {"SFO", "ATL"}, {"ATL", "ICN"}, {"ATL","SFO"}};
System.out.println((new P43164()).solution(tickets));
}
}