public bool FindRoutes(int startBusStop, int finishBusStop, int startTime) { fastRoute.Clear(); cheapRoute.Clear(); startPoint = startBusStop; endPoint = finishBusStop; this.startTime = startTime; RoutePoint firstPoint = new RoutePoint(); firstPoint.PrevPoint = null; firstPoint.Point = startPoint; firstPoint.Cost = 0; firstPoint.Bus = -1; firstPoint.Time = startTime; List <int> listBusIndexes = new List <int>(); for (int i = 0; i < busCount; i++) { listBusIndexes.Add(i); } return(NextPoint(listBusIndexes, firstPoint, -1)); }
public RoutePoint nextStop(RoutePoint curStop, int nextBus) { int lastLapTime = curStop.Time > this.startTime ? curStop.Time - (curStop.Time - this.startTime) % RoundTime : this.startTime; if (!busStops.Contains(curStop.Point)) { return(new RoutePoint(-2, nextBus, -2, -2, curStop)); } else { int i = 0; for (; lastLapTime < curStop.Time || (busStops[i % busStops.Count] != curStop.Point && TimeConverter.timeIsCorrect(lastLapTime)); ) { for (i = 0; i < BusStops.Count && !(lastLapTime >= curStop.Time && busStops[i] == curStop.Point); i++) { lastLapTime += intervals[i]; } } int curPointIndex = i; curStop.StartTime = lastLapTime; return(new RoutePoint(busStops[(curPointIndex + 1) % busStops.Count], nextBus, lastLapTime + intervals[curPointIndex % busStops.Count], Cost, curStop)); } }
public RoutePoint(RoutePoint curPoint) { this.point = curPoint.Point; this.bus = curPoint.Bus; this.time = curPoint.Time; this.cost = curPoint.Cost; this.prevPoint = curPoint.PrevPoint == null ? null : (RoutePoint)curPoint.PrevPoint.MemberwiseClone(); }
public RoutePoint(int point, int bus, int time, int cost, RoutePoint prevPoint) { this.point = point; this.bus = bus; this.time = time; this.cost = cost; this.prevPoint = prevPoint; }
private List <RoutePoint> HistoryToList(RoutePoint lastPoint) { List <RoutePoint> routeList = new List <RoutePoint>(); RoutePoint tempPoint = lastPoint; for (; tempPoint != null;) { routeList.Add(tempPoint); tempPoint = tempPoint.PrevPoint; } routeList.Reverse(); return(routeList); }
public bool NextPointAnotherBus(List <int> avaliableBuses, RoutePoint curPoint, int bus) { avaliableBuses.Remove(bus); RoutePoint nextPoint = busArray[bus].nextStop(curPoint, bus); if (nextPoint.Point < 0) { return(false); } else { nextPoint.Cost += curPoint.Cost; return(NextPoint(avaliableBuses, nextPoint, busArray[bus].BusStops.Count - 1)); } }
public bool NextPoint(List <int> avaliableBuses, RoutePoint curPoint, int stopsCnt) { bool result = false; if (curPoint.Point == endPoint) { if (TimeConverter.timeIsCorrect(curPoint.Time)) { if (fastRoute.Count == 0) { fastRoute = HistoryToList(curPoint); result = true; } else if (fastRoute[fastRoute.Count - 1].Time > curPoint.Time) { fastRoute = HistoryToList(curPoint); result = true; } if (cheapRoute.Count == 0) { cheapRoute = HistoryToList(curPoint); result = true; } else if (cheapRoute[cheapRoute.Count - 1].Cost > curPoint.Cost) { cheapRoute = HistoryToList(curPoint); result = true; } } } else { if (curPoint.Point != startPoint) { result |= NextPointThisBus(avaliableBuses, new RoutePoint(curPoint), stopsCnt - 1); } foreach (int bus in avaliableBuses) { result |= NextPointAnotherBus(new List <int>(avaliableBuses), new RoutePoint(curPoint), bus); } } return(result); }
public bool NextPointThisBus(List <int> avaliableBuses, RoutePoint curPoint, int stopsCnt) { if (stopsCnt <= 0) { return(false); } else { RoutePoint nextPoint = busArray[curPoint.Bus].nextStop(curPoint); if (nextPoint.Point < 0) { return(false); } else { nextPoint.Cost = curPoint.Cost; return(NextPoint(avaliableBuses, nextPoint, stopsCnt)); } } }
public RoutePoint nextStop(RoutePoint curStop) { return(nextStop(curStop, curStop.Bus)); }