-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTravelplan_backtracking.java
149 lines (122 loc) · 3.46 KB
/
Travelplan_backtracking.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import java.util.ArrayList;
import java.util.List;
public class Travelplan_backtracking {
Integer[][] hours;
String[] c_remained = { "NP", "IS", "CA", "UK", "US" };
private void build_hour_table() {
// NP: 0 (North Pole)
// IS: 1
// CA: 2
// UK: 3
// US: 4
this.hours = new Integer[5][5];
hours[0][0] = 0; // NP -> NP
hours[0][1] = 14; // NP -> IS
hours[0][2] = 15; // NP -> CA
hours[0][3] = 17; // NP -> UK
hours[0][4] = 16; // NP -> US
hours[1][0] = 14; // IS -> NP
hours[1][1] = 0; // IS -> IS
hours[1][2] = 24; // IS -> CA
hours[1][3] = 8; // IS -> UK
hours[1][4] = 36; // IS -> US
hours[2][0] = 15; // CA -> NP
hours[2][1] = 24; // CA -> IS
hours[2][2] = 0; // CA -> CA
hours[2][3] = 34; // CA -> UK
hours[2][4] = 4; // CA -> US
hours[3][0] = 17; // UK -> NP
hours[3][1] = 8; // UK -> IS
hours[3][2] = 34; // UK -> CA
hours[3][3] = 0; // UK -> UK
hours[3][4] = 30; // UK -> US
hours[4][0] = 16; // US -> NP
hours[4][1] = 36; // US -> IS
hours[4][2] = 4; // US -> CA
hours[4][3] = 30; // US -> UK
hours[4][4] = 0; // US -> US
}
public Integer get_hour(String start, String end) {
Integer x = get_index(start);
Integer y = get_index(end);
return this.hours[x][y];
}
private Integer get_index(String str) {
if (str.equals("NP"))
return 0;
if (str.equals("IS"))
return 1;
if (str.equals("CA"))
return 2;
if (str.equals("UK"))
return 3;
if (str.equals("US"))
return 4;
return null;
}
// enumeration -> backtracking
List<String> route = new ArrayList<>();
public void backtracking(Integer constraint_hour) {
String c_start = "NP";
route.add(c_start);
c_remained[0] = null;
backtracking_recursion(constraint_hour);
}
private void backtracking_recursion(Integer constraint_hour) {
int hour_total = get_hour_total();
if (route.size() == 5) {
if (hour_total < constraint_hour) {
print_result(hour_total);
} else {
System.out.print("[X]: ");
print_result(hour_total);
}
} else {
// backtracked
if (hour_total >= constraint_hour) {
System.out.print("[backtracked]: ");
print_result(hour_total);
return;
}
}
/** step01: just pick any child to continue each round **/
for (int i = 0; i < c_remained.length; i++) {
if (c_remained[i] == null)
continue;
String c_next = c_remained[i];
route.add(c_next);
c_remained[i] = null;
backtracking_recursion(constraint_hour);
route.remove(c_next);
c_remained[i] = c_next;
}
}
private int get_hour_total() {
if (route.size() == 0)
return 0;
int hour_total = 0;
String c_start = route.get(0);
String c_end = null;
for (int i = 1; i < route.size(); i++) {
c_end = route.get(i);
hour_total += get_hour(c_start, c_end);
c_start = c_end;
}
return hour_total;
}
private void print_result(int hour_total) {
for (int i = 0; i < route.size(); i++) {
System.out.print(route.get(i));
if (i != route.size() - 1) {
System.out.print("->");
}
}
System.out.println(" : " + hour_total);
}
public static void main(String[] args) {
Travelplan_backtracking tp = new Travelplan_backtracking();
tp.build_hour_table();
Integer hour_constraint = 60; // 65 ~ 100
tp.backtracking(hour_constraint);
}
}