You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
4.0 KiB

6 years ago
using System;
using System.IO;
using System.Diagnostics;
using Combinatorics.Collections;
using System.Collections.Generic;
6 years ago
using static System.Console;
namespace komvo
{
class Program
{
static void Main(string[] args)
{
6 years ago
Beginig:
List<Point> Points = new List<Point>();
Random random = new Random();
int pCount;
Write("Введите количество точек: ");
pCount = Convert.ToInt32(ReadLine());
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
6 years ago
6 years ago
for(int i = 0; i < pCount; i++)
6 years ago
{
6 years ago
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);
6 years ago
}
6 years ago
WriteLine("\n");
6 years ago
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++)
6 years ago
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));
6 years ago
for (int i = 0; i < matr; i++)
6 years ago
{
for (int j = 0; j < matr; j++) Write("{0:00.00} ", Matrix[i, j]);
6 years ago
WriteLine();
}
6 years ago
int[] toSwap = new int[Points.Count - 1];
for (int i = 0; i <= toSwap.Length - 1; i++) toSwap[i] = i + 2;
6 years ago
//for (int i = 0; i < toSwap.Length; i++) Write(toSwap[i]); //debug
6 years ago
int index = 0;
double opLength = 0, prevResult = 0;
string opRoute = string.Empty;
Permutations<int> perm = new Permutations<int>(toSwap);
foreach(IList<int> p in perm)
{
6 years ago
string route = string.Empty;
for (int i = 0; i < toSwap.Length; i++) route += p[i].ToString() + ";";
opLength = CountLength(Matrix, route);
6 years ago
6 years ago
if (index != 0)
6 years ago
{
6 years ago
if (opLength > prevResult)
6 years ago
{
6 years ago
opLength = prevResult;
opRoute = route;
6 years ago
}
}
6 years ago
else prevResult = opLength;
index++;
6 years ago
}
6 years ago
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();
6 years ago
6 years ago
Write("Ещё раз? (y/n)");
char answer = Convert.ToChar(ReadLine());
if (answer == 'y')
6 years ago
{
6 years ago
Points.Clear();
goto Beginig;
6 years ago
}
}
6 years ago
public static double CountLength(double[,] mtr, string route)
{
6 years ago
double result = 0;
string[] index = route.Split(';');
for (int j = 0; j < index.Length - 1; j++)
{
6 years ago
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)
{
6 years ago
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];
}
}
6 years ago
return result;
}
public partial struct Point
6 years ago
{
public int X { get; set; }
public int Y { get; set; }
}
static int Fact(int a) => a <= 1 ? 1 : a * Fact(a - 1);
6 years ago
}
}