void ShowResualt(StopCity stack = null) { string res = ""; if (stack == null) { res = "Шлях не знайдено"; } else { res = stack.ToString(); StopCity tmp = stack.last; while (tmp != null) { res = tmp.ToString() + "-" + res; tmp = tmp.last; } } MessageBox.Show(res); }
private void Search(object sender, EventArgs e) { if (this.start.Text.Equals("") || this.end.Text.Equals("")) { return; } City start = null; City end = null; foreach (City tmp in Logist.City.cities) { if (tmp.ToString().Equals(this.start.Text)) { start = tmp; } if (tmp.ToString().Equals(this.end.Text)) { end = tmp; } } if (start == null || end == null) { return; } if (start.Equals(end)) { return; } StopCity iter = new StopCity(start); SortedSet <City> drove = new SortedSet <City>(); drove.Add(start); SortedSet <Route> droveRoute = new SortedSet <Logist.Route>(); SortedSet <StopCity> now = new SortedSet <StopCity>(); foreach (Route tmp in start.getRoutes()) { foreach (City c in tmp.getCities()) { if (!drove.Contains(c)) { if (c.Equals(end)) { ShowResualt(new StopCity(c, iter)); return; } now.Add(new StopCity(c, iter)); } } droveRoute.Add(tmp); } while (now.Count != 0) { SortedSet <StopCity> next = new SortedSet <StopCity>(); foreach (StopCity nowCity in now) { drove.Add(nowCity); foreach (Route tmp in nowCity.getRoutes()) { if (droveRoute.Contains(tmp)) { continue; } droveRoute.Add(tmp); foreach (City c in tmp.getCities()) { if (!drove.Contains(c) && !now.Contains(c) && !next.Contains(c)) { next.Add(new StopCity(c, nowCity)); if (c.Equals(end)) { ShowResualt(new StopCity(c, nowCity)); return; } } } } } now = next; } ShowResualt(); }
public StopCity(City city, StopCity last = null) : base(city.ToString()) { this.routes = city.getRoutes(); this.last = last; }