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.

182 lines
6.1 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
{
public static List<Point> Points = new List<Point>();
public static List<string> Infinitives = new List<string>();
6 years ago
static void Main(string[] args)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
6 years ago
string file = "coord.txt";
using (StreamReader cr = new StreamReader(file))
{
string line;
while ((line = cr.ReadLine()) != null)
{
string[] str = line.Split();
Points.Add(new Point { X = int.Parse(str[0]), Y = int.Parse(str[1]) });
6 years ago
WriteLine(str[0] + " " + str[1]);
6 years ago
}
}
6 years ago
WriteLine();
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++) WriteLine(toSwap[i]);
6 years ago
6 years ago
int fct = Fact(Points.Count - 1);
6 years ago
6 years ago
string[] routes = new string[fct];
6 years ago
double[] leng = new double[fct];
6 years ago
double optim = leng[0];
int oIndex = 0;
Permutations<int> perm = new Permutations<int>(toSwap);
int index = 0;
foreach(IList<int> p in perm)
{
string res = string.Empty;
6 years ago
for (int i = 0; i < toSwap.Length; i++) res += p[i].ToString() + ";";
routes[index++] = res;
}
6 years ago
6 years ago
//for (int i = 0; i < routes.Length; i++) WriteLine(routes[i]);
6 years ago
6 years ago
//CountLen(ref leng, Matrix, routes);
CountLen2(ref leng, Matrix, routes);
SearchBest(ref optim, ref oIndex, routes, leng);
6 years ago
//Debug(routes);
WriteLine("\n\n1{1}1 - оптимальный маршрут длинной {0:00.00}\nВсего маршрутов {2}\n{4} {3}",
6 years ago
optim, routes[oIndex], routes.Length, fct, Points.Count);
WriteLine(stopwatch.ElapsedMilliseconds + "ms");
stopwatch.Stop();
6 years ago
ReadKey();
}
6 years ago
public static void CountLen2(ref double[] result, double[,] mtr, string[] rts)
{
//WriteLine("loading 00%");
for (int i = 0; i < rts.Length; i++)
{
double res = 0;
//char[] ch = rts[i].ToCharArray();
//CursorLeft -= 3;
//int percent = i / rts.Length * 100;
//Write("{0:00}%");
string[] index = rts[i].Split(';');
for (int j = 0; j < index.Length - 1; j++)
{
string dg = index[j];
int db = int.Parse(index[j]) - 2; //debug
string db1 = rts[i].ToString(); //debug
if (j == 0) res += mtr[0, int.Parse(index[j]) - 1];
else if (j == index.Length - 1)
{
res += mtr[int.Parse(index[j - 1]) - 2, int.Parse(index[j]) - 2];
res += mtr[0, int.Parse(index[j]) - 2];
}
else
{
res += mtr[int.Parse(index[j - 1]) - 2, int.Parse(index[j]) - 2];
}
}
result[i] = res;
}
}
public static void CountLen (ref double[] result, double[,] mtr, string[] rts)
6 years ago
{
for (int i = 0; i < rts.Length; i++)
{
double res = 0;
6 years ago
char[] ch = rts[i].ToCharArray();
6 years ago
for (int j = 0; j < ch.Length; j++)
{
6 years ago
int db = int.Parse(ch[j].ToString()) - 1; //debug
string db1 = rts[i].ToString(); //debug
6 years ago
if (j == 0) res += mtr[0, int.Parse(ch[j].ToString()) - 1];
6 years ago
else if (j == ch.Length - 1)
{
6 years ago
res += mtr[int.Parse(ch[j - 1].ToString()) - 1, int.Parse(ch[j].ToString()) - 1];
res += mtr[0, int.Parse(ch[j].ToString()) - 1];
6 years ago
}
else
{
6 years ago
res += mtr[int.Parse(ch[j - 1].ToString()) - 1, int.Parse(ch[j].ToString()) - 1];
6 years ago
}
}
result[i] = res;
}
}
6 years ago
public static void SearchBest(ref double optim, ref int oIndex, string[] routes, double[] leng)
{
optim = leng[0];
oIndex = 0;
for (int i = 0; i < leng.Length; i++)
{
6 years ago
//WriteLine("{0:N2} - {1}", leng[i], routes[i]);
if (optim > leng[i])
{
optim = leng[i];
oIndex = i;
}
}
}
6 years ago
public static void Debug (string[] routes)
{
for (int i = 0; i < routes.Length; i++)
{
for (int j = i + 1; j < routes.Length - 1; j++)
if (routes[i].Equals(routes[j])) WriteLine("{0} {2}:: {1} {3}!!!!", routes[i], routes[j], i, j);
}
}
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
}
}