-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoPathRecursive.java
More file actions
47 lines (40 loc) · 976 Bytes
/
Copy pathGoPathRecursive.java
File metadata and controls
47 lines (40 loc) · 976 Bytes
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
package GoingToPath;
import java.util.ArrayList;
import java.util.List;
public class GoPathRecursive {
private List<List<String>> points;
private List<Integer> path;
public GoPathRecursive(List<List<String>> points) {
this.points = points;
initPath();
}
public void goThroughPath() {
goThroughPath(0);
}
private void goThroughPath(int order) {
if (order < points.size()-1) {
for (int i = 0; i < points.get(order).size(); i++) {
path.set(order, i);
goThroughPath(order+1);
}
}else {
for (int i = 0; i < points.get(order).size(); i++) {
path.set(order, i);
printPath(path);
}
}
}
public void printPath(List<Integer> path) {
// path等於要去points拿的位置的排列
for (int i = 0; i < path.size(); i++) {
System.out.print(path.get(i) + ", ");
}
System.out.println();
}
private void initPath() {
path = new ArrayList<Integer>();
for (int i = 0; i < points.size(); i++) {
path.add(0);
}
}
}