1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
| using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
public static int size = 5;
public static double[,] ArrayDistance = new double[size, size];
static void Main(string[] args)
{
// прочитать первые 10 строк файла
string[] lines = File.ReadAllLines(@"C:\input.txt").Take(5).ToArray();
// разобрать в массив
for (int i = 0; i < size; i++)
{
int[] row = lines[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToArray();
for (int j = 0; j < size; j++)
{
if (i != j && row[j]==0)
{
ArrayDistance[i, j] = int.MaxValue;
}
else
{
ArrayDistance[i, j] = row[j];
}
}
}
List<String> city = new List<string>();
string[] line = File.ReadAllLines(@"C:\input2.txt", Encoding.Default).Take(5).ToArray();
// разобрать в массив
for (int j = 0; j < size; j++)
{
city.Add(line[j]);
}
Console.WriteLine("Ниже приведен список городов: ");
for (int i = 0; i < city.Count; i++)
{
Console.WriteLine("{0} - {1}", i, city[i]);
}
String count_route = (city.Count - 1).ToString();
Console.WriteLine("Выберите из какого города стартует маршрут (0-{0}) : ", count_route);
String from_string = Console.ReadLine();
Console.WriteLine("Выберите в какой город просчитать маршрут (0-{0}) : ", (city.Count - 1));
String to_string = Console.ReadLine();
int from = 0;
int to = 0;
try
{
from = Int32.Parse(from_string);
to = Int32.Parse(to_string);
if (from > (city.Count - 1) && to > (city.Count - 1))
throw new Exception();
if (from == to)
{
Console.Clear();
Console.WriteLine("Расстояние между городами 0км.");
Console.ReadLine();
return;
}
}
catch (Exception)
{
Console.Clear();
Console.WriteLine("Вы ввели не правильное число. Работа программы прекращена.");
Console.ReadLine();
return;
}
Console.Clear();
Console.WriteLine("Поиск кратчайшего пути...");
double result = SearchShortWay(from, to, new List<int>());
Console.WriteLine("Растояние между {0} и {1} = {2}", city[from], city[to], result);
Console.ReadLine();
}
public static double SearchShortWay(int index_from, int index_to, List<int> used_city)
{
double result = 0;
List<double> shortway = new List<double>();
for (int i = 0; i < 5; i++)
{
bool flag = false;
foreach (int index in used_city)
{
if (i == index)
flag = true;
}
if (flag)
continue;
if (i == index_from)
continue;
if (i != index_to )
{
double part_way = ArrayDistance[index_from, i];
used_city.Add(index_from);
shortway.Add(part_way + SearchShortWay(i, index_to, used_city));
}
else
{
shortway.Add(ArrayDistance[index_to, index_from]);
}
if (index_to == 3 && index_from == 1)
{
shortway.Add(ArrayDistance[1, 2] + ArrayDistance[2, 3]);
}
}
result = shortway.Min();
return result;
}
}
} |