日期:2014-05-18 浏览次数:20610 次
findRoutes(起始节点, 终止节点, 当前线路) {
对起始节点的每个下一节点做如下操作: {
如果节点是终止节点, 则将节点添加到当前线路,加入线路的List中, continue;
如果节点已在当前线路中, continue;
否则, 将节点加入当前线路, 以节点为开始节点,终止节点不变,加入该节点后的线路为新的当前线路, 递归获得线路的List;
}
返回线路的List;
}
class Route {
private List<String> route;
public Route() {
route = new ArrayList<String>();
}
public List<String> getRoute() {
return route;
}
public int length() {
return this.route.size();
}
public void addStop(String stop) {
route.add(stop);
}
public boolean containsStop(String stop) {
return route.contains(stop) ? true : false;
}
public Route copyRoute() {
Route copy = new Route();
for (String stop : route)
copy.addStop(stop);
return copy;
}
public void printRoute() {
System.out.println("----------");
System.out.print("Route length: " + route.size() + "; ");
for (int i = 0; i < route.size(); i++) {
System.out.print(route.get(i));
if (i != route.size() - 1)
System.out.print("-->");
}
}
}
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("1", "3,4");
map.put("2", "3,4");
map.put("3", "1,2,4");
map.put("4", "1,2,3")