Пример #1
0
 private static CitiesData ParseFromLines(string[] lines)
 {
     var result = new CitiesData();
     var n = int.Parse(lines[0].Trim());
     var distances = new double[n,n];
     for (int i = 0; i < n; i++)
     {
         var currentLineSplit = lines[i+1].Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
         if (currentLineSplit.Length != n) 
             throw new Exception("Количество аргументов в строке != n");
         for (int j = 0; j < n; j++)
         {
             var distance = double.Parse(currentLineSplit[j], CultureInfo.InvariantCulture);
             distances[i, j] = distance;
         }
     }
     result.CitiesDistances = distances;
     return result;
 }
Пример #2
0
        public static CitiesData GenerateCitiesDataCircle(double circleRadius, int citiesCount)
        {
            CitiesData result = new CitiesData();
            var citiesPosition = new double[citiesCount][];
            var anglePerCiti = Math.PI*2/citiesCount;
            var angle = 0.0;
            for (int positionCounter = 0; positionCounter < citiesCount; positionCounter++)
            {
                citiesPosition[positionCounter] = new[]
                {
                    circleRadius*Math.Cos(angle),
                    circleRadius*Math.Sin(angle)
                };
                angle += anglePerCiti;
            }
            result.CitiesDistances = new double[citiesCount,citiesCount];
            for (int i = 0; i < citiesCount; i++)
            {
                for (int j = 0; j < citiesCount; j++)
                {
                    result.CitiesDistances[i, j] = Math.Sqrt(

                        (citiesPosition[i][0] - citiesPosition[j][0]) * (citiesPosition[i][0] - citiesPosition[j][0]) 
                        +
                        (citiesPosition[i][1] - citiesPosition[j][1]) * (citiesPosition[i][1] - citiesPosition[j][1])
                        );
                }
            }
            var resultPrice = 0.0;
            for (int i = 0; i < citiesCount-1; i++)
            {
                resultPrice += result.CitiesDistances[i, i + 1];
            }
            resultPrice += result.CitiesDistances[citiesCount-1, 0];
            Console.WriteLine("Optimal path: " + Math.Round(resultPrice,2).ToString("0.00"));
            return result;

        }