#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define NUM_CITIES 6
void swap(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}
int calculateDistance(int *route, int distances[NUM_CITIES][NUM_CITIES]) {
int totalDistance = 0;
for (int i = 0; i < NUM_CITIES; i++) {
totalDistance += distances[route[i]][route[(i + 1) % NUM_CITIES]];
}
return totalDistance;
}
void copyArray(int *source, int *destination, int length) {
for (int i = 0; i < length; i++) {
destination[i] = source[i];
}
}
void permute(int *array, int start, int end, int distances[NUM_CITIES][NUM_CITIES], int *shortestRoute, int *minDistance) {
if (start == end) {
int currentDistance = calculateDistance(array, distances);
if (currentDistance < *minDistance) {
*minDistance = currentDistance;
copyArray(array, shortestRoute, NUM_CITIES);
}
} else {
for (int i = start; i < end; i++) {
swap((array + start), (array + i));
permute(array, start + 1, end, distances, shortestRoute, minDistance);
swap((array + start), (array + i));
}
}
}
int main() {
int distances[NUM_CITIES][NUM_CITIES] = {