public void TestLargeCity() { City city = new City(new Point2D(Point2D.MinValue, Point2D.MinValue), new Point2D(Point2D.MaxValue, Point2D.MaxValue), new RandomBuilder(1337)); Assert.AreEqual(4, city.Roads.Count); Assert.AreEqual(4, city.Institutions.Count); foreach (Institution institution in city.Institutions) { Assert.False(float.IsNaN(institution.Location.X)); Assert.False(float.IsNaN(institution.Location.Y)); } int roadsCount = 400; int currentRoad = 0; for (int currentX = 0; currentRoad < roadsCount; currentX += 10) { city.AddRoad(new Road(new Point2D(currentX, Point2D.MinValue), new Point2D(currentX, Point2D.MaxValue))); currentRoad++; } Assert.AreEqual(roadsCount + 4, city.Roads.Count); CityPathInfo cityPathInfo = Finder.FindPathTo(city, city.Institutions[0], InstitutionType.PoliceDepartment); Assert.AreEqual(2, cityPathInfo.Path.Length); }
public static void Main(string[] args) { string inputFilePath; if (args.Length < 2) { Console.WriteLine("Пожалуйста, укажите путь к входному файлу."); inputFilePath = Console.ReadLine(); } else { inputFilePath = args[1]; } CityInputInfo cityInputInfo = ReadCityInputInfoFromFile(inputFilePath); if (cityInputInfo == null) { return; } City city = BuildCity(cityInputInfo); Console.WriteLine("Городские дороги: "); foreach (Road road in city.Roads) { Console.WriteLine(road.ToString()); } while (true) { if (!TryAskStartPoint(city, out var startPoint)) { return; } if (!TryAskEndPoint(out var institutionType)) { return; } CityPathInfo pathInfo = Finder.FindPathTo(city, startPoint, institutionType); if (pathInfo.IsEmptyPath()) { Console.WriteLine("Не удалось найти путь."); return; } Console.WriteLine($"Путь найден: {pathInfo}. Общая продолжительность {pathInfo.TotalLength}"); Console.WriteLine("Нажмите любую клавишу для продолжения..."); Console.ReadKey(); } }
public void TestFindPathToOverload() { City city = DataGenerator.GenerateTestCity(); // Проверка перегрузки метода поиска, который принимает город и объект учреждения, а не граф. // Результаты должны быть идентичны версии с графом. Graph graph = DataGenerator.GenerateTestCityGraph(); PathInfo graphPathInfo = Finder.FindPathTo(graph, graph.Vertices.First(), InstitutionType.Hospital); CityPathInfo cityPathInfo = Finder.FindPathTo(city, city.Institutions.First(), InstitutionType.Hospital); Assert.AreEqual(graphPathInfo.Path.Length, cityPathInfo.Path.Length); Assert.AreEqual(graphPathInfo.TotalLength, cityPathInfo.TotalLength); }
/// <summary> /// Осуществляет поиск кратчайшего пути до необходимого типа постройки в «городе». /// Если построек такого типа несколько, то возвращает путь только до той, который оказался самым кратчайшим. /// Если не удалось найти, то возвращает пустой путь. /// </summary> /// <param name="city">Объект с городом, поиск пути в котором необходимого выполнить.</param> /// <param name="startInstitution">Стартовый объект, из которой ищем путь.</param> /// <param name="institutionType">Тип постройки, к которой ищем путь.</param> /// <returns>Объект с информацией о найденном или не найденном пути.</returns> public static CityPathInfo FindPathTo(City.City city, Institution startInstitution, InstitutionType institutionType) { Graph graph = ConvertCityToGraph(city); Vertex startVertex = graph.Vertices.FirstOrDefault(vertex => vertex.Tag == startInstitution); if (startVertex == null) { throw new ArgumentException("'startInstitution' is not found in the 'city' object", nameof(startInstitution)); } PathInfo pathInfo = FindPathTo(graph, startVertex, institutionType); CityPathInfo cityPathInfo = new CityPathInfo( pathInfo.Path.Select(vertex => vertex.Tag as Institution).ToArray(), pathInfo.TotalLength); return(cityPathInfo); }