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.

208 lines
6.6 KiB

6 years ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
6 years ago
using System.IO;
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();
}
string formP = string.Empty;
6 years ago
for (int i = 2; i <= Points.Count; i++) formP += i.ToString();
6 years ago
int fct = Fact(Points.Count - 1);
WriteLine("\n{0} {1}\n\n\n", formP, fct);
6 years ago
6 years ago
int[] routes = new int[fct];
double[] leng = new double[fct];
6 years ago
6 years ago
int waves = fct / 6;
double optim = leng[0];
int oIndex = 0;
string inf = formP;
Swapper(0, inf, ref routes, waves);
CountLen(ref leng, Matrix, routes);
SearchBest(ref optim, ref oIndex, routes, leng);
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();
}
public static string Swap(string value, int first, int second)
{
char[] res = value.ToCharArray();
res[first] = value[second];
res[second] = value[first];
value = new string(res);
return value;
}
public static string Swap(string value, int first, int second, int debug)
{
int d = debug;
char[] res = value.ToCharArray();
res[first] = value[second];
res[second] = value[first];
value = new string(res);
return value;
}
6 years ago
6 years ago
public static void CountLen (ref double[] result, double[,] mtr, int[] rts)
{
for (int i = 0; i < rts.Length; i++)
{
double res = 0;
char[] ch = rts[i].ToString().ToCharArray();
for (int j = 0; j < ch.Length; j++)
{
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];
else if (j == ch.Length - 1)
{
res += mtr[int.Parse(ch[j - 1].ToString()) - 1, int.Parse(ch[j].ToString()) - 1];
res += mtr[0, int.Parse(ch[j].ToString()) - 1];
}
else
{
res += mtr[int.Parse(ch[j - 1].ToString()) - 1, int.Parse(ch[j].ToString()) - 1];
}
}
result[i] = res;
}
}
public static void Swapper(int counter, string inf, ref int[] routes, int waves)
{
string formP = inf;
if (counter >= waves) WriteLine("\nEnd");
else
{
if (counter == 0)
{
Infinitives.Add(inf);
formP = Infinitives[0];
}
else if (counter % 4 == 0)
{
Infinitives.Add(Swap(inf, counter / 4 - 1, inf.Length - 1, counter));
formP = Infinitives[counter / 4];
}
else if (waves > 1)
{
formP = Swap(Infinitives[counter / 4], counter % 4 - 1, 3);
}
BlockOfSix(formP, ref routes, counter);
Swapper(counter + 1, inf, ref routes, waves);
}
}
public static void BlockOfSix(string formP, ref int[] routes, int start)
{
for (int j = 0; j < 6; j++)
{
if (j == 0)
{
routes[j + start * 6] = int.Parse(formP);
WriteLine(routes[j + start * 6]);
}
else if (j % 2 == 0)
{
routes[j + start * 6] = int.Parse(formP = Swap(formP, 0, j / 2));
WriteLine(routes[j + start * 6]);
}
else
{
routes[j + start * 6] = int.Parse(Swap(formP, 1, 2));
WriteLine(routes[j + start * 6]);
}
}
}
public static void SearchBest(ref double optim, ref int oIndex, int[] routes, double[] leng)
{
optim = leng[0];
oIndex = 0;
for (int i = 0; i < leng.Length; i++)
{
WriteLine("{0:N2} - {1}", leng[i], routes[i]);
if (optim > leng[i])
{
optim = leng[i];
oIndex = i;
}
}
}
public static void Debug (int[] 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
}
}