using System; using System.IO; using System.Diagnostics; using Combinatorics.Collections; using System.Collections.Generic; using static System.Console; namespace komvo { class Program { static void Main(string[] args) { Beginig: List Points = new List(); Random random = new Random(); int pCount; Write("Введите количество точек: "); pCount = Convert.ToInt32(ReadLine()); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); for(int i = 0; i < pCount; i++) { int x = random.Next(80); int y = random.Next(70); Points.Add(new Point { X = x, Y = y }); WriteLine("Точка {0} [{1};{2}]", i + 1, x, y); } WriteLine("\n"); int matr = Points.Count; double[,] Matrix = new double[matr, matr]; for (int i = 0; i < Points.Count; i++) for (int j = i; j < Points.Count - 1; j++) Matrix[i, j + 1] = Matrix[j + 1, i] = Math.Sqrt(Math.Pow(Points[j + 1].X - Points[i].X, 2) + Math.Pow(Points[j + 1].Y - Points[i].Y, 2)); for (int i = 0; i < matr; i++) { for (int j = 0; j < matr; j++) Write("{0:00.00} ", Matrix[i, j]); WriteLine(); } int[] toSwap = new int[Points.Count - 1]; for (int i = 0; i <= toSwap.Length - 1; i++) toSwap[i] = i + 2; //for (int i = 0; i < toSwap.Length; i++) Write(toSwap[i]); //debug int index = 0; double opLength = 0, prevResult = 0; string opRoute = string.Empty; Permutations perm = new Permutations(toSwap); foreach(IList p in perm) { string route = string.Empty; for (int i = 0; i < toSwap.Length; i++) route += p[i].ToString() + ";"; opLength = CountLength(Matrix, route); if (index != 0) { if (opLength > prevResult) { opLength = prevResult; opRoute = route; } } else prevResult = opLength; index++; } WriteLine("\n\n1;{1}1 - оптимальный маршрут длинной {0:00.00}\nИз {2} маршрутов при {3} точках\nПосчитано за {4} мс\n\n", opLength, opRoute, Fact(Points.Count-1), Points.Count, stopwatch.ElapsedMilliseconds); stopwatch.Stop(); Write("Ещё раз? (y/n)"); char answer = Convert.ToChar(ReadLine()); if (answer == 'y') { Points.Clear(); goto Beginig; } } public static double CountLength(double[,] mtr, string route) { double result = 0; string[] index = route.Split(';'); for (int j = 0; j < index.Length - 1; j++) { string dg = index[j]; int db = int.Parse(index[j]) - 2; //debug if (j == 0) result += mtr[0, int.Parse(index[j]) - 1]; else if (j == index.Length - 1) { result += mtr[int.Parse(index[j - 1]) - 2, int.Parse(index[j]) - 2]; result += mtr[0, int.Parse(index[j]) - 2]; } else { result += mtr[int.Parse(index[j - 1]) - 2, int.Parse(index[j]) - 2]; } } return result; } public partial struct Point { public int X { get; set; } public int Y { get; set; } } static int Fact(int a) => a <= 1 ? 1 : a * Fact(a - 1); } }