|
|
@ -1,5 +1,6 @@ |
|
|
|
using System; |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Collections.Generic; |
|
|
|
|
|
|
|
using System.Diagnostics; |
|
|
|
using System.IO; |
|
|
|
using System.IO; |
|
|
|
using static System.Console; |
|
|
|
using static System.Console; |
|
|
|
|
|
|
|
|
|
|
@ -8,9 +9,12 @@ namespace komvo |
|
|
|
class Program |
|
|
|
class Program |
|
|
|
{ |
|
|
|
{ |
|
|
|
public static List<Point> Points = new List<Point>(); |
|
|
|
public static List<Point> Points = new List<Point>(); |
|
|
|
|
|
|
|
public static List<string> Infinitives = new List<string>(); |
|
|
|
|
|
|
|
|
|
|
|
static void Main(string[] args) |
|
|
|
static void Main(string[] args) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
Stopwatch stopwatch = new Stopwatch(); |
|
|
|
|
|
|
|
stopwatch.Start(); |
|
|
|
string file = "coord.txt"; |
|
|
|
string file = "coord.txt"; |
|
|
|
|
|
|
|
|
|
|
|
using (StreamReader cr = new StreamReader(file)) |
|
|
|
using (StreamReader cr = new StreamReader(file)) |
|
|
@ -29,13 +33,10 @@ namespace komvo |
|
|
|
double[,] Matrix = new double[matr, matr]; |
|
|
|
double[,] Matrix = new double[matr, matr]; |
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < Points.Count; i++) |
|
|
|
for (int i = 0; i < Points.Count; i++) |
|
|
|
{ |
|
|
|
|
|
|
|
for (int j = i; j < Points.Count - 1; j++) |
|
|
|
for (int j = i; j < Points.Count - 1; j++) |
|
|
|
{ |
|
|
|
|
|
|
|
Matrix[i, j + 1] = Matrix[j + 1, i] = |
|
|
|
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)); |
|
|
|
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 i = 0; i < matr; i++) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -47,107 +48,27 @@ namespace komvo |
|
|
|
for (int i = 2; i <= Points.Count; i++) formP += i.ToString(); |
|
|
|
for (int i = 2; i <= Points.Count; i++) formP += i.ToString(); |
|
|
|
|
|
|
|
|
|
|
|
int fct = Fact(Points.Count - 1); |
|
|
|
int fct = Fact(Points.Count - 1); |
|
|
|
WriteLine("{0} {1}", formP, fct); |
|
|
|
WriteLine("\n{0} {1}\n\n\n", formP, fct); |
|
|
|
WriteLine("\n\n"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int[] routes = new int[fct]; |
|
|
|
int[] routes = new int[fct]; |
|
|
|
double[] leng = new double[fct]; |
|
|
|
double[] leng = new double[fct]; |
|
|
|
|
|
|
|
|
|
|
|
int waves = fct / 6; |
|
|
|
int waves = fct / 6; |
|
|
|
|
|
|
|
|
|
|
|
int cn = 1, cn2 = 0; |
|
|
|
|
|
|
|
string inf = formP; |
|
|
|
|
|
|
|
for (int i = 1; i < waves + 1; i++) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
for(int j = 0; j < 6; j++) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (i == 0 && j == 0) { routes[j + (i - 1) * 6] = int.Parse(formP); WriteLine(routes[j + (i - 1) * 6]); } |
|
|
|
|
|
|
|
else if (j % 2 == 0) { routes[j + (i - 1) * 6] = int.Parse(formP = Swap(formP, 0, j / 2)); WriteLine(routes[j + (i - 1) * 6]); } |
|
|
|
|
|
|
|
else { routes[j + (i - 1) * 6] = int.Parse(Swap(formP, 1, 2)); WriteLine(routes[j + (i - 1) * 6]); } |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (waves > 1) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (i % 4 == 0 && waves > 4) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
formP = Swap(formP, cn2++, 4); |
|
|
|
|
|
|
|
WriteLine("{0} {1}", formP, inf); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
switch (cn) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
case 1: |
|
|
|
|
|
|
|
formP = Swap(formP, '2', '5'); |
|
|
|
|
|
|
|
cn++; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case 2: |
|
|
|
|
|
|
|
formP = Swap(formP, '3', '2'); |
|
|
|
|
|
|
|
cn++; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case 3: |
|
|
|
|
|
|
|
formP = Swap(formP, '4', '3'); |
|
|
|
|
|
|
|
cn++; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
WriteLine("???????"); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (cn > 3) cn = 1; |
|
|
|
|
|
|
|
//formP = Swap(inf, cn++, 3); |
|
|
|
|
|
|
|
//WriteLine("{0} {1}", formP, inf); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//if (cn > 4) |
|
|
|
|
|
|
|
//{ |
|
|
|
|
|
|
|
// cn = 0; |
|
|
|
|
|
|
|
// formP = Swap(inf, cn2, 4); |
|
|
|
|
|
|
|
// cn2++; |
|
|
|
|
|
|
|
//} |
|
|
|
|
|
|
|
//else |
|
|
|
|
|
|
|
//{ |
|
|
|
|
|
|
|
//formP = Swap(inf, cn, 3); |
|
|
|
|
|
|
|
//} |
|
|
|
|
|
|
|
//cn++; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CountLen(ref leng, Matrix, routes); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
double optim = leng[0]; |
|
|
|
double optim = leng[0]; |
|
|
|
int oIndex = 0; |
|
|
|
int oIndex = 0; |
|
|
|
for (int i = 0; i < leng.Length; i++) |
|
|
|
string inf = formP; |
|
|
|
{ |
|
|
|
|
|
|
|
WriteLine("{0:N2} - {1}", leng[i], routes[i]); |
|
|
|
|
|
|
|
if (optim > leng[i]) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
optim = leng[i]; |
|
|
|
|
|
|
|
oIndex = i; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Swapper(0, inf, ref routes, waves); |
|
|
|
|
|
|
|
CountLen(ref leng, Matrix, routes); |
|
|
|
|
|
|
|
SearchBest(ref optim, ref oIndex, routes, leng); |
|
|
|
|
|
|
|
Debug(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); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
WriteLine("\n\n1{1}1 - оптимальный маршрут длинной {0:00.00}\nВсего маршрутов {2}\n{4} {3}", |
|
|
|
WriteLine("\n\n1{1}1 - оптимальный маршрут длинной {0:00.00}\nВсего маршрутов {2}\n{4} {3}", |
|
|
|
optim, routes[oIndex], routes.Length, fct, Points.Count); |
|
|
|
optim, routes[oIndex], routes.Length, fct, Points.Count); |
|
|
|
|
|
|
|
WriteLine(stopwatch.ElapsedMilliseconds + "ms"); |
|
|
|
|
|
|
|
stopwatch.Stop(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//WriteLine(Fact(5)); |
|
|
|
|
|
|
|
//MessageBox.Show(Math.Sqrt(-1).ToString()); |
|
|
|
|
|
|
|
ReadKey(); |
|
|
|
ReadKey(); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static string Swap(string value, int first, int second) |
|
|
|
public static string Swap(string value, int first, int second) |
|
|
@ -160,14 +81,12 @@ namespace komvo |
|
|
|
return value; |
|
|
|
return value; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static string Swap(string value, char first, char second) |
|
|
|
public static string Swap(string value, int first, int second, int debug) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int f, s; |
|
|
|
int d = debug; |
|
|
|
char[] res = value.ToCharArray(); |
|
|
|
char[] res = value.ToCharArray(); |
|
|
|
WriteLine(f = value.IndexOf(first)); |
|
|
|
res[first] = value[second]; |
|
|
|
WriteLine(s = value.IndexOf(second)); |
|
|
|
res[second] = value[first]; |
|
|
|
res[value.IndexOf(first)] = value[value.IndexOf(second)]; |
|
|
|
|
|
|
|
res[value.IndexOf(second)] = value[value.IndexOf(first)]; |
|
|
|
|
|
|
|
value = new string(res); |
|
|
|
value = new string(res); |
|
|
|
|
|
|
|
|
|
|
|
return value; |
|
|
|
return value; |
|
|
@ -182,6 +101,8 @@ namespace komvo |
|
|
|
char[] ch = rts[i].ToString().ToCharArray(); |
|
|
|
char[] ch = rts[i].ToString().ToCharArray(); |
|
|
|
for (int j = 0; j < ch.Length; j++) |
|
|
|
for (int j = 0; j < ch.Length; j++) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
int db = int.Parse(ch[j].ToString()) - 1; //debug |
|
|
|
|
|
|
|
string db1 = rts[i].ToString(); //debug |
|
|
|
if (j == 0) res += mtr[0, int.Parse(ch[j].ToString()) - 1]; |
|
|
|
if (j == 0) res += mtr[0, int.Parse(ch[j].ToString()) - 1]; |
|
|
|
else if (j == ch.Length - 1) |
|
|
|
else if (j == ch.Length - 1) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -197,6 +118,84 @@ namespace komvo |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|
public partial struct Point |
|
|
|
{ |
|
|
|
{ |
|
|
|
public int X { get; set; } |
|
|
|
public int X { get; set; } |
|
|
|