BackTracking function is not working as expected

微笑、不失礼 提交于 2020-01-25 10:13:31

问题


I am trying to solve the following question using BackTracking in C but I don't know how to continue from here...

The question is:

Chris is planning to travel in a country with N cities. He will get help from a matrix NxN that the cell (I,J) represents the length of the road from city I to City J. The length of the road from City A to city B is not the same when compared to the road from City B to City A. The Road From the same source city back to it (directly) is 0. Chris noticed that the shortest road From A to B isn't alway the direct one between both Cities. You will need to help Chris Finding the shortest path. Write a function that checks for the shortest map given a matrix NxN which stores the values of the roads lengths. Note: N is defined as 4.

Example:

The Shortest path from 0 to 1 is going to City 0 then 3 then 1 if given the following matrix:

0 5 2 2

1 0 1 1

1 2 0 1

1 1 2 0

Her's my code:

int ShortestPath (int SourceCity, int DestinationCity, int Distance [][N], bool Chosen[][N])
{
    int Path=0;
    if (SourceCity==DestinationCity)
    {
        Distance[SourceCity][DestinationCity]=true;
        return 0;
    }

    for (int i=0;i<N;i++)
    {
        for (int j=0;j<N;j++)
        {
            Path += Distance[i][j];
            if (!Chosen[i][j])
            {
                Chosen[i][j] = true;
                ShortestPath(i, DestinationCity, Distance, Chosen);
            }
        }
    }
    if (Path>=Distance[SourceCity][DestinationCity])
    {
        Chosen[SourceCity,DestinationCity]=false;
        return Distance[SourceCity][DestinationCity];
    }
}

Note: the matrix chosen indicated wether I chose a specific road or not (Its initial values are all false)


回答1:


Here is a suggested solution. It is an implementation of the Dijkstra algorithm finding the shortest path.

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <stdbool.h>

// number of nodes
#define N 4
// distance matrix from i to j for d[i][j]
int d[N][N] = {
    {0, 5, 2, 2},
    {1, 0, 1, 1},
    {1, 2, 0, 1},
    {1, 1, 2, 0},
};


// shortestPath stores in path the shortest path from start node to 
// final node using the distance matrix d. It returns the number of 
// nodes in the path.
int shortestPath(int d[N][N], int start, int final, int path[N]){
   // node previous to node i
   int prev[N];

   // initialize distance from node i to start as infinite
   int dist[N];
   for (int i = 0; i < N; i++)
       dist[i] = INT_MAX;
   dist[start] = 0;

   // initialize list of nodes done
   bool done[N];
   for (int i = 0; i < N; i++)
       done[i] = false;
   int nDone = 0;

   // while we haven’t done all nodes
   while (nDone < N) {
        // find not yet done node with minimal distance to start node
        int minDist = INT_MAX;
        int n; // node with minimum distance
        for (int i = 0; i < N; i++)
            if (!done[i] && dist[i] < minDist)
                minDist = dist[n = i];
        done[n] = true;
        nDone++;
        // we can stop when final node is done
        if (n == final)
            break;

        // for every node j...
        for (int j = 0; j < N; j++) {
            // if node j is not yet done, 
            // and distance from start to j through n is smaller to known
            if (!done[j] && dist[j] > dist[n] + d[n][j]) {
                // set new shortest distance
                dist[j] = dist[n] + d[n][j];
                // set node n as previous to node j
                prev[j] = n;
            }
        }
    }

    // get path [start, ..., final]
    int j = N;
    for (int i = final; i != start; i = prev[i])
        path[--j] = i;
    path[--j] = start;
    if (j == 0)
        return N;
    int n = N-j;
    for (int i = 0; i < n; i++, j++)
        path[i] = path[j];
    return n;
}

int main() {
    int path[N];
    int n = shortestPath(d, 0, 1, path);

    printf("path: %d", path[0]);
    for (int i = 1; i < n; i++)
        printf("->%d", path[i]);
    printf("\n");
    return 0;
}

It outputs

path: 0->3->1


来源:https://stackoverflow.com/questions/59689774/backtracking-function-is-not-working-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!