50,530
社区成员
发帖
与我相关
我的任务
分享
import java.util.HashSet;
public class TestNode1 {
public static void main(String[] args) {
int[][] distance = { { 5, 7, 4 }, { 6, 5 } };
HashSet<Integer> setG = new HashSet<Integer>();
for (int i = 0; i < distance.length; i++) {
HashSet<Integer> temp = new HashSet<Integer>();
temp.addAll(setG);
setG.clear();
for (int dd : distance[i]) {
if (i == 0) {
setG.add(dd);
}
for (int d : temp) {
setG.add(d + dd);
}
}
}
System.out.println(setG);
}
}
import java.util.HashSet;
import java.util.Set;
public class TestNode1 {
static Set setG = new HashSet();// 用来装不同的 可行的 通路长度
public static void main(String[] args) {
int[] d4 = { 8, 4, 3 };
int[] d3 = { 1, 2 };
int[] d2 = { 10, 9, 8, 4 };
int[] d1 = { 5, 8, 10 };
Node n5 = new Node(null, null);// 终点
Node n4 = new Node(d4, n5);
Node n3 = new Node(d3, n4);
Node n2 = new Node(d2, n3);
Node n1 = new Node(d1, n2);
search(n1, n3, 0);
System.out.println(setG);// 输出可行的通路长度
}
static void search(Node start, Node end, int dis) {
if (start.next != null) {
for (int distance : start.distanceToNext) {
int temp = dis + distance;
search(start.next, end, temp);
}
} else {
setG.add(dis);
}
}
}
class Node {
int[] distanceToNext;// 到下一个节点的各个距离
Node next;// 下一个节点
public Node(int[] d, Node next) {
distanceToNext = d;
this.next = next;
}
}
import java.util.HashSet;
import java.util.Set;
public class TestNode {
static Set setG = new HashSet();// 用来装不同的 可行的 通路长度
public static void main(String[] args) {
int[] d1 = { 5, 7, 4 };
int[] d2 = { 5, 6 };
Node n3 = new Node(null, null);//终点
Node n2 = new Node(d2, n3);
Node n1 = new Node(d1, n2);
search(n1, n3, 0);
System.out.println(setG);// 输出可行的通路长度
}
static void search(Node start, Node end, int dis) {
if (start.next != null) {
for (int distance : start.distanceToNext) {
int temp = dis + distance;
search(start.next, end, temp);
}
} else {
setG.add(dis);
}
}
}
class Node {
int[] distanceToNext;// 到下一个节点的各个距离
Node next;//下一个节点
public Node(int[] d, Node next) {
distanceToNext = d;
this.next = next;
}
}