简体   繁体   English

回溯功能未按预期工作

[英]BackTracking function is not working as expected

I am trying to solve the following question using BackTracking in C but I don't know how to continue from here...我正在尝试使用 C 中的 BackTracking 解决以下问题,但我不知道如何从这里继续......

The question is:问题是:

Chris is planning to travel in a country with N cities.克里斯计划在一个有 N 个城市的国家旅行。 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.他将从矩阵 NxN 中得到帮助,其中单元格 (I,J) 表示从城市 I 到城市 J 的道路长度。从城市 A 到城市 B 的道路长度与从城市的道路相比是不同的城市 B 到城市 A。从同一个源城市回到它(直接)的道路是 0。克里斯注意到从 A 到 B 的最短道路并不总是两个城市之间的直接道路。 You will need to help Chris Finding the shortest path.您将需要帮助 Chris 找到最短路径。 Write a function that checks for the shortest map given a matrix NxN which stores the values of the roads lengths.编写一个函数,检查给定矩阵 NxN 的最短地图,该矩阵存储道路长度的值。 Note: N is defined as 4.注:N 定义为 4。

Example:例子:

The Shortest path from 0 to 1 is going to City 0 then 3 then 1 if given the following matrix:如果给定以下矩阵,从 0 到 1 的最短路径是去城市 0 然后是 3 然后是 1:

0 5 2 2 0 5 2 2

1 0 1 1 1 0 1 1

1 2 0 1 1 2 0 1

1 1 2 0 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)注意:选择的矩阵表示我是否选择了特定的道路(其初始值均为false)

Here is a suggested solution.这是一个建议的解决方案。 It is an implementation of the Dijkstra algorithm finding the shortest path.它是寻找最短路径的 Dijkstra 算法的实现。

#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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM