public void CheckLineSwitch(StationObject current, StationObject next, StationObject previous) { bool switched = true; for (int a = 0; a < current.GetLines.Count; a++) { string currentLine = current.GetLines[a]; for (int b = 0; b < previous.GetLines.Count; b++) { string previousLine = previous.GetLines[b]; for (int c = 0; c < next.GetLines.Count; c++) { string nextLine = next.GetLines[c]; if (previousLine == currentLine && currentLine == nextLine) { switched = false; } } } } if (switched) { lineSwitches++; } }
public RouteObject(StationObject originStation, StationObject destination) { this.originStation = originStation; this.route = new List <StationObject>(); //AddStopToRoute(originStation); this.destination = destination; }
public string FindShortestRoutes(MetroMapObject map, string start, string end) { StationObject starting = GetStation(map, start); StationObject ending = GetStation(map, end); RouteObject routeFromOrigin = new RouteObject(starting, ending); routeFromOrigin.AddStopToRoute(starting); RouteBundle shortestRoutes = ExporeAllBranches(routeFromOrigin, new RouteBundle(), map); return(shortestRoutes.PrintResult()); }
private StationObject GetStation(MetroMapObject mapObject, string name) //simple method { for (int i = 0; i < mapObject.StationsCount; i++) { StationObject calledStation = mapObject.Stations[i]; if (name == calledStation.Name) { return(calledStation); } } return(null); }
private RouteBundle ExporeAllBranches(RouteObject seedRoute, RouteBundle allRoutes, MetroMapObject mapE) { if (debug) { Console.WriteLine($"smallest path so far: {lowestPathSoFar}"); } StationObject currentStation = seedRoute.CurrentStation(); //seedRoute.AddStopToRoute(currentStation); if (debug) { Console.WriteLine($"length of route: {seedRoute.Distance}"); } if (debug) { Console.WriteLine($"~~~~~current station~~~~~: {currentStation.Name}"); } //the for loop below is to handle any station with more than 1 stop left to explore, //either dumping it or finding a route if (debug) { Console.WriteLine($"current station # of contacts: {currentStation.NumberOfContacts}"); } for (int s = 0; s < currentStation.NumberOfContacts; s++) { if (debug) { Console.WriteLine($"location in for loop: <{s}>"); } //Console.WriteLine($"destination name: {seedRoute.Destination.Name}"); StationObject addStation = GetStation(mapE, currentStation.ConnectionList[s]); if (debug) { Console.WriteLine($"next station: [{addStation.Name}]"); } if (addStation.Name == seedRoute.Destination.Name) { allRoutes.AddRoute(seedRoute); if (seedRoute.Distance >= 3) { seedRoute.CheckLineSwitch(currentStation, addStation, seedRoute.PreviousStation()); } if (debug) { Console.WriteLine($"# # # # NEW BRANCH ADDED # # # #"); } if (seedRoute.Distance <= lowestPathSoFar) { lowestPathSoFar = seedRoute.Distance; } if (debug) { Console.WriteLine($"Is new record?: {seedRoute.Distance <= lowestPathSoFar}"); } if (debug) { Console.WriteLine($"smallest path so far: {lowestPathSoFar}"); } continue; } if (debug) { Console.WriteLine($"next station # of contacts: {addStation.NumberOfContacts}"); Console.WriteLine($"enters recursive loop: {seedRoute.Distance + 1 < lowestPathSoFar && addStation.NumberOfContacts > 1 && !seedRoute.AlreadyCheckedThisStation(addStation)}"); Console.WriteLine($"already checked the next station? {seedRoute.AlreadyCheckedThisStation(addStation)}"); } if (seedRoute.Distance + 1 < lowestPathSoFar && addStation.NumberOfContacts > 1 && !seedRoute.AlreadyCheckedThisStation(addStation)) { RouteObject clone = seedRoute.CloneRoute(); clone.AddStopToRoute(addStation); if (clone.Distance >= 3) { clone.CheckLineSwitch(currentStation, addStation, seedRoute.PreviousStation()); } allRoutes = ExporeAllBranches(clone, allRoutes, mapE); } if (debug) { Console.WriteLine($"/////current station post loop/////: {currentStation.Name}"); } } //below is the part of code that should only be accessed by branches with a single station remaining. StationObject nextStation = GetStation(mapE, currentStation.ConnectionList[0]); if (debug) { Console.WriteLine($"next station: {nextStation.Name}"); Console.WriteLine($"next station # of contacts: {nextStation.NumberOfContacts}"); } string dest = seedRoute.Destination.Name; if (seedRoute.Distance + 1 <= lowestPathSoFar && nextStation.Name == dest) { seedRoute.AddStopToRoute(nextStation); if (seedRoute.Distance >= 3) { seedRoute.CheckLineSwitch(currentStation, nextStation, seedRoute.PreviousStation()); } allRoutes.AddRoute(seedRoute); lowestPathSoFar = seedRoute.Distance; if (debug) { Console.WriteLine($"smallest path so far: {lowestPathSoFar}"); } } allRoutes.PruneLongRoutes(); return(allRoutes); }
public bool AlreadyCheckedThisStation(StationObject checkThis) { return(route.Contains(checkThis)); }
public void AddStopToRoute(StationObject stop) { route.Add(stop); distance += 1; }
public MetroMapObject() { this.stations = new List <StationObject>(); //this is the entire list of stations entered into a map object StationObject a = new StationObject("A"); a.AddConnection("B"); a.OnLines("red"); stations.Add(a); // StationObject b = new StationObject("B"); b.AddConnection("A"); b.AddConnection("C"); b.AddConnection("H"); b.OnLines("black"); b.OnLines("red"); stations.Add(b); // StationObject c = new StationObject("C"); c.AddConnection("D"); c.AddConnection("B"); c.AddConnection("J"); c.AddConnection("K"); c.OnLines("red"); c.OnLines("green"); stations.Add(c); // StationObject d = new StationObject("D"); d.AddConnection("E"); d.AddConnection("C"); d.AddConnection("L"); d.AddConnection("J"); d.OnLines("red"); stations.Add(d); // StationObject e = new StationObject("E"); e.AddConnection("D"); e.AddConnection("F"); e.AddConnection("J"); e.AddConnection("M"); e.OnLines("red"); stations.Add(e); // StationObject f = new StationObject("F"); f.AddConnection("J"); f.AddConnection("E"); f.AddConnection("G"); f.OnLines("red"); f.OnLines("black"); stations.Add(f); // StationObject h = new StationObject("H"); h.AddConnection("J"); h.AddConnection("B"); h.OnLines("black"); stations.Add(h); // StationObject j = new StationObject("J"); j.AddConnection("H"); j.AddConnection("C"); j.AddConnection("D"); j.AddConnection("E"); j.AddConnection("F"); j.AddConnection("O"); j.OnLines("black"); j.OnLines("blue"); j.OnLines("green"); stations.Add(j); // StationObject o = new StationObject("O"); o.AddConnection("J"); o.OnLines("blue"); stations.Add(o); //k StationObject k = new StationObject("K"); k.AddConnection("C"); k.AddConnection("L"); k.OnLines("green"); stations.Add(k); //l StationObject l = new StationObject("L"); l.AddConnection("D"); l.AddConnection("K"); l.AddConnection("M"); l.AddConnection("N"); l.OnLines("blue"); l.OnLines("green"); stations.Add(l); //n StationObject n = new StationObject("N"); n.AddConnection("L"); n.OnLines("green"); n.OnLines("blue"); stations.Add(n); //m StationObject m = new StationObject("M"); m.AddConnection("L"); m.AddConnection("E"); m.OnLines("blue"); m.OnLines("green"); stations.Add(m); //g StationObject g = new StationObject("G"); g.AddConnection("F"); g.OnLines("black"); stations.Add(g); }