BackTracking函数无法正常工作

问题描述 投票:0回答:1

我正在尝试使用C中的BackTracking解决以下问题,但我不知道如何从这里继续...

问题是:

克里斯正计划在一个有N个城市的国家旅行。他将从矩阵NxN获得帮助,该单元格(I,J)代表从城市I到城市J的道路长度。从城市A到城市B的道路长度与从城市B到城市A的道路长度相比有所不同。从同一来源城市返回(直接)返回的道路为0。克里斯注意到,从A到B的最短道路始终不是两个城市之间的直接道路。您将需要帮助Chris查找最短路径。编写一个函数,在给定矩阵NxN的情况下检查最短地图,该矩阵存储道路长度的值。注意:N定义为4。

示例:

如果给定以下矩阵,则从0到1的最短路径将到达城市0,然后是3,然后是1:

0 5 2 2

1 0 1 1

1 2 0 1

1 1 2 0

她是我的代码:

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];
    }
}

注意:选择的矩阵表示我是否选择了一条特定的道路(其初始值全为假)

c algorithm recursion c99 backtracking
1个回答
0
投票

这里是建议的解决方案。这是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;
   done[start] = true;
   int nDone = 1;

   // 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;
}

输出

path: 0->3->1
© www.soinside.com 2019 - 2024. All rights reserved.