Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static final int ArraySize = 6;
- static int Size; // Размер матрицы, считываем с консоли
- static int MaxWeight; // Размер максимального пути, считываем с консоли
- static Random rand = new Random();
- static int MinLong = 10000; // Максимальное расстояние между городами
- static int step; // Шаг алгоритма или на какую вершину мы переходим
- static int count = 0; // Счётчик
- static int path = 0; // Сумма пути
- static int[][] MapArray = new int[ArraySize][ArraySize]; // Граф смежности
- static boolean[] Visited = new boolean[ArraySize]; // Массив пройденных вершин
- static int[] Way = new int[ArraySize]; // Массив пройденных вершин
- public static void MainMethod(int CurrentVertex) {
- Visited[CurrentVertex] = true;
- for(int j = 0; j < Size; j++) {
- if ( (MapArray[CurrentVertex][j] != 0) && (!Visited[j]) ) {
- if (MapArray[CurrentVertex][j] < MinLong) {
- MinLong = MapArray[CurrentVertex][j];
- step = j;
- }
- count++;
- // Way[CurrentVertex] = step; // Записываем в массив путь из вершин
- // path = path + MapArray[CurrentVertex][step]; // Записываем сумму пути
- }
- path = path + MapArray[CurrentVertex][step]; // Записываем сумму пути
- Way[CurrentVertex] = step; // Записываем в массив путь из вершин
- if (count == (Size)) { // Прошли по всем вершинам
- path = path + MapArray[step][0]; // Записываем путь из предпоследней вершины, в последнюю
- }
- MainMethod(CurrentVertex);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment