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.

138 lines
4.2 KiB

6 years ago
using System;
using System.Diagnostics;
using Combinatorics.Collections;
using System.Collections.Generic;
6 years ago
using static System.Console;
6 years ago
using System.Threading.Tasks;
6 years ago
namespace komvo
{
class Program
{
static void Main(string[] args)
{
6 years ago
Beginig:
List<Point> Points = new List<Point>();
Random random = new Random();
6 years ago
int pCount = 0;
6 years ago
Write("Введите количество точек: ");
6 years ago
Retry:
try
{
pCount = Convert.ToInt32(ReadLine());
}
catch
{
Write("Введите натуральное число: ");
goto Retry;
}
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
bool first = true;
6 years ago
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
int[] route = new int[Points.Count - 1];
for (int i = 0; i < toSwap.Length; i++) route[i] = p[i];
prevResult = CountLength(Matrix, route);
//WriteLine(opLength);
6 years ago
6 years ago
if (first)
6 years ago
{
6 years ago
opLength = prevResult;
first = false;
6 years ago
}
6 years ago
6 years ago
if(opLength > prevResult)
{
opLength = prevResult;
opRoute = string.Empty;
foreach (int i in route) opRoute += i + ";";
}
6 years ago
}
6 years ago
//int[] toSwapNew = new int[Points.Count - 1];
//for (int i = 0; i <= toSwap.Length - 1; i++) toSwapNew[i] = i + 2;
//foreach (int i in toSwapNew) Write(i + " ");
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)");
6 years ago
char answer = ReadKey().KeyChar;
6 years ago
if (answer == 'y')
6 years ago
{
6 years ago
Points.Clear();
6 years ago
WriteLine("\n");
6 years ago
goto Beginig;
6 years ago
}
}
6 years ago
public static double CountLength(double[,] mtr, int[] route)
{
6 years ago
double result = 0;
6 years ago
for (int j = 0; j < route.Length - 1; j++)
{
6 years ago
if (j == 0) result += mtr[0, route[j] - 1];
else if (j == route.Length - 1)
{
6 years ago
result += mtr[route[j - 1] - 2, route[j] - 2];
result += mtr[0, route[j] - 2];
6 years ago
}
else
{
6 years ago
result += mtr[route[j - 1] - 2, route[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
}
}