Problem mit den zu langen Routen gefixt.
Wir müssen die Straßen als genutzt markieren, nicht die Städte.planning
parent
a34d60c0d0
commit
225d4bddfe
|
@ -7,7 +7,9 @@ public class NaviApp {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Routeplaner map = new Routeplaner();
|
Routeplaner map = new Routeplaner();
|
||||||
|
|
||||||
|
System.out.println();
|
||||||
System.out.println(map.getNumberOfCities());
|
System.out.println(map.getNumberOfCities());
|
||||||
|
System.out.println();
|
||||||
|
|
||||||
for (String waypoint : map.initializePlanning("Mannheim", "Wilhelmshaven"))
|
for (String waypoint : map.initializePlanning("Mannheim", "Wilhelmshaven"))
|
||||||
System.out.println(waypoint);
|
System.out.println(waypoint);
|
||||||
|
|
|
@ -6,7 +6,6 @@ public class City {
|
||||||
private String name;
|
private String name;
|
||||||
private int distance;
|
private int distance;
|
||||||
private String predecessor;
|
private String predecessor;
|
||||||
private boolean visited;
|
|
||||||
|
|
||||||
private TreeSet<Street> neighbors = new TreeSet<>();
|
private TreeSet<Street> neighbors = new TreeSet<>();
|
||||||
|
|
||||||
|
@ -31,6 +30,7 @@ public class City {
|
||||||
if (distance < this.distance) {
|
if (distance < this.distance) {
|
||||||
this.distance = distance;
|
this.distance = distance;
|
||||||
this.predecessor = predecessor;
|
this.predecessor = predecessor;
|
||||||
|
System.out.println(name + " " + distance + " <- " + predecessor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,14 +42,6 @@ public class City {
|
||||||
this.distance = distance;
|
this.distance = distance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isVisited() {
|
|
||||||
return visited;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setVisited() {
|
|
||||||
visited = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getPredecessor() {
|
public String getPredecessor() {
|
||||||
return predecessor;
|
return predecessor;
|
||||||
}
|
}
|
||||||
|
@ -57,7 +49,6 @@ public class City {
|
||||||
private void reset() {
|
private void reset() {
|
||||||
distance = Integer.MAX_VALUE;
|
distance = Integer.MAX_VALUE;
|
||||||
predecessor = null;
|
predecessor = null;
|
||||||
visited = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -46,27 +46,28 @@ public class Routeplaner {
|
||||||
private void planRoute(String start, String destination) {
|
private void planRoute(String start, String destination) {
|
||||||
System.out.println("Prüfe " + start);
|
System.out.println("Prüfe " + start);
|
||||||
City sc = cities.get(start);
|
City sc = cities.get(start);
|
||||||
sc.setVisited();
|
|
||||||
|
|
||||||
if (sc == null || cities.get(destination) == null)
|
if (sc == null || cities.get(destination) == null)
|
||||||
throw new RuntimeException("Stadt nicht gefunden.");
|
throw new RuntimeException("Stadt nicht gefunden.");
|
||||||
|
|
||||||
for (Street s : sc.getNeighbors()) {
|
for (Street s : sc.getNeighbors()) {
|
||||||
|
if (s.isUsed())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
s.setUsed();
|
||||||
|
|
||||||
System.out.println(s.toString());
|
System.out.println(s.toString());
|
||||||
|
|
||||||
City nc = s.getDestination();
|
City nc = s.getDestination();
|
||||||
|
|
||||||
if (nc.isVisited()) {
|
|
||||||
System.out.println("schon besucht");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
nc.checkAndChangeDistance(sc.getDistance() + s.getDistance(), sc.getName());
|
nc.checkAndChangeDistance(sc.getDistance() + s.getDistance(), sc.getName());
|
||||||
|
|
||||||
if (nc.getName().equals(destination)) {
|
if (nc.getName().equals(destination)) {
|
||||||
System.out.println("Route nach " + destination + " gefunden mit Entfernung: " + nc.getDistance());
|
System.out.println("Route nach " + destination + " gefunden mit Entfernung: " + nc.getDistance());
|
||||||
} else {
|
} else {
|
||||||
|
System.out.println();
|
||||||
planRoute(nc.getName(), destination);
|
planRoute(nc.getName(), destination);
|
||||||
|
System.out.println();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -142,6 +143,7 @@ public class Routeplaner {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
System.out.println(start + " " + line + " " + matrix[cityLookup.get(start)][cityLookup.get(line)]);
|
||||||
this.addStreet(start, line, matrix[cityLookup.get(start)][cityLookup.get(line)]);
|
this.addStreet(start, line, matrix[cityLookup.get(start)][cityLookup.get(line)]);
|
||||||
|
|
||||||
} // while
|
} // while
|
||||||
|
|
|
@ -5,6 +5,8 @@ public class Street implements Comparable<Street> {
|
||||||
private final City destination;
|
private final City destination;
|
||||||
|
|
||||||
private final int distance;
|
private final int distance;
|
||||||
|
private boolean used;
|
||||||
|
|
||||||
|
|
||||||
public Street(City start, City destination, int distance) {
|
public Street(City start, City destination, int distance) {
|
||||||
super();
|
super();
|
||||||
|
@ -39,6 +41,13 @@ public class Street implements Comparable<Street> {
|
||||||
return distance;
|
return distance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isUsed() {
|
||||||
|
return used;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUsed() {
|
||||||
|
used = true;
|
||||||
|
}
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return start.getName() + " -> " + destination.getName() + ": " + distance + " km";
|
return start.getName() + " -> " + destination.getName() + ": " + distance + " km";
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue