public MRTRoute(MRTExit from, MRTLink[] links) { this.from = from; this.links = links; this.Description = from.name; string lastRouteID = this.links[0].routeID; if (this.links.Count() < 2) { this.Description += string.Format(" -{0}-> {1}", this.links.Last().routeID, this.links.Last().to.name); return; } for (int x = 1; x < this.links.Count(); x++) { MRTLink link = this.links[x]; if (link.routeID != lastRouteID) { this.Description += string.Format(" -{0}-> {1}", lastRouteID, this.links[x - 1].to.name); TransionCount++; } lastRouteID = link.routeID; } this.Description += string.Format(" -{0}-> {1}", this.links.Last().routeID, this.links.Last().to.name); }
public MRTRoute[] FindRoutes(string from, string to) { if (from == null) { throw new Exception("from is null"); } else if (to == null) { throw new Exception("to is null"); } else if (from == to) { throw new Exception("Begin and destination should be different."); } MRTExit fromExit = this.exits[from]; MRTExit toExit = this.exits[to]; if (fromExit == null) { throw new Exception("Unable to find this exit as begin."); } else if (toExit == null) { throw new Exception("Unable to find this exit as destination."); } MRTRouteFinder finder = new MRTRouteFinder(fromExit, toExit); return(finder.routes); }
private void Load() { string data = MRTData.data; foreach (string line in data.Split('\r')) { string[] components = line.Split(','); if (components.Length == 3) { string routeID = components[0]; string from = components[1]; string to = components[2]; if (!this.exits.ContainsKey(from)) { this.exits.Add(from, new MRTExit(from)); } if (!this.exits.ContainsKey(to)) { this.exits.Add(to, new MRTExit(to)); } MRTExit fromExit = this.exits[from]; MRTExit toExit = this.exits[to]; fromExit.AddLink(routeID, toExit); toExit.AddLink(routeID, fromExit); } } }
public MRTRouteFinder(MRTExit fromExit, MRTExit toExit) { this.fromExit = fromExit; this.toExit = toExit; this.travelLinksOfExit(fromExit); this.foundRoutes.Sort((a, b) => a.CompareTo(b)); }
void travelLinksOfExit(MRTExit exit) { MRTLink[] links = exit.links.ToArray(); foreach (MRTLink link in links) { if (link.to == this.toExit) { visitedLinks.Push(link); List <MRTLink> copy = this.visitedLinks.ToList(); copy.Reverse(); foundRoutes.Add(new MRTRoute(this.fromExit, copy.ToArray())); visitedLinks.Pop(); } else if (!visitedExits.Contains(link.to)) { visitedExits.Push(exit); visitedLinks.Push(link); travelLinksOfExit(link.to); visitedExits.Pop(); visitedLinks.Pop(); } } }
public void AddLink(string routeID, MRTExit to) { MRTLink link = new MRTLink(routeID, to); this.links.Add(link); }