Calculate the shortest possible distance

2

In an exercise, I need to print all possible paths between cities, where cities are represented by x and y coordinates and the last city is the city of departure. Remembering that you must find the smallest paths to each different starting city. That part of the exercise was done. For example, for four cities, I have the following output:

24
(1,10)     (4,4)     (5,1)     (2,0)     (1,10)
(1,10)     (4,4)     (2,0)     (5,1)     (1,10)
(1,10)     (5,1)     (4,4)     (2,0)     (1,10)
(1,10)     (5,1)     (2,0)     (4,4)     (1,10)
(1,10)     (2,0)     (5,1)     (4,4)     (1,10)
(1,10)     (2,0)     (4,4)     (5,1)     (1,10)
(4,4)     (1,10)     (5,1)     (2,0)     (4,4)
(4,4)     (1,10)     (2,0)     (5,1)     (4,4)
(4,4)     (5,1)     (1,10)     (2,0)     (4,4)
(4,4)     (5,1)     (2,0)     (1,10)     (4,4)
(4,4)     (2,0)     (5,1)     (1,10)     (4,4)
(4,4)     (2,0)     (1,10)     (5,1)     (4,4)
(5,1)     (4,4)     (1,10)     (2,0)     (5,1)
(5,1)     (4,4)     (2,0)     (1,10)     (5,1)
(5,1)     (1,10)     (4,4)     (2,0)     (5,1)
(5,1)     (1,10)     (2,0)     (4,4)     (5,1) 
(5,1)     (2,0)     (1,10)     (4,4)     (5,1)
(5,1)     (2,0)     (4,4)     (1,10)     (5,1)
(2,0)     (4,4)     (5,1)     (1,10)     (2,0)
(2,0)     (4,4)     (1,10)     (5,1)     (2,0)
(2,0)     (5,1)     (4,4)     (1,10)     (2,0)
(2,0)     (5,1)     (1,10)     (4,4)     (2,0)
(2,0)     (1,10)     (5,1)     (4,4)     (2,0)
(2,0)     (1,10)     (4,4)     (5,1)     (2,0)

Being 24 is the total of possibilities.

After this part, I need to indicate the best path, that is, the path with the shortest distance, using the Euclidean distance formula between two points. I created two functions to perform this calculation, as can be seen below:

float Distancia(Cidade cA, Cidade cB)
{
    int distanciaX = pow(cA.x - cB.x, 2);
    int distanciaY = pow(cA.y - cB.y, 2);
    float distancia = sqrt(distanciaX + distanciaY);
    return distancia;
}

float Distancias(Cidade *C, int numeroCidades)
{
    int i;
    float total = 0;
    for(i = 0; i < numeroCidades; i++)
    {
        if(i == numeroCidades - 1)
        {
            total = total + Distancia(C[i], C[0]);
        }
        else
        {
            total = total + Distancia(C[i], C[i+1]);
        }
    }
    return total;
}

To find the shortest distance, I implemented the following function:

float MenorDistancia(Cidade *C, int numeroCidade)
{
    int i;
    float distancias[numeroCidade];
    float menor = distancias[0];
    for(i = 0; i < numeroCidade; i++)
    {
        distancias[i] = Distancias(C, numeroCidade);
        if(distancias[i] < menor)
            menor = distancias[i];
    }
    return menor;
}

But now I'm in doubt on how to implement these functions in the running of the program as a whole. The first question I have is whether the functions are correct. If they are, I would like to know if I can use them soon in the function that creates these path possibilities, which is the following function:

void Permuta(FILE *saida, Cidade *C, int *sequencia, int inicio, int 
termino)
{
    int i, j;
    if(inicio == termino)
    {
        for(i = 0; i < termino; i++)
        {
            fprintf(saida, "(%d,%d)     ", C[sequencia[i]].x, 
C[sequencia[i]].y);
        }
        fprintf(saida, "(%d,%d)\n", C[sequencia[0]].x, C[sequencia[0]].y);
    }
    else
    {  
        for(j = inicio; j < termino; j++)
        {
            Troca((sequencia+inicio), (sequencia+j));
            Permuta(saida, C, sequencia, inicio+1, termino);
            Troca((sequencia+inicio), (sequencia+j));
        }
    }
}

I would like to know how to use functions in the same loop that prints the paths and / or if there is a better way to do it.

    
asked by anonymous 21.09.2017 / 15:50

0 answers