【问题标题】:Finding cheapest road with recursion用递归找到最便宜的路
【发布时间】:2019-02-15 11:22:59
【问题描述】:

我需要使用递归找到最便宜的道路。
道路从点 1 开始,它需要经过所有其他点 (2,3,4,5) 并返回到点 1。每个点之间的行程成本在一个二维数组(地图)中。

╔═══════════════════════╗
║    1|2|3|4|5|(points) ║
║ 1--0 1 3 4 2          ║
║ 2--1 0 4 2 6          ║
║ 3--3 4 0 7 1          ║
║ 4--4 2 7 0 7          ║
║ 5--2 6 1 7 0          ║
║ (points)              ║
╚═══════════════════════╝

这意味着例如从第 1 点到第 5 点有 2 个“金钱”成本,从第 5 点到第 4 点的成本是 7。每次在地图中切换“方向”。我认为尝试次数应该等于 25(4! + 1)

void FindingRoad(int[][] map, int[] pointsOrder, ref int tripCost, ref int attempts)
{

    if (attempts == 0)
    {
        return;
    }
    int tempCost = 0;
    int[] tempPointsOrder = new int[5];
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 5; j++)
        {

        }
    }
    if (tempCost<tripCost)
    {
        tripCost = tempCost;
        pointsOrder = tempPointsOrder;
    }
    attempts--;
    FindingRoad(map,pointsOrder,ref tripCost,attempts);
}

预期道路(点顺序)输出应为 1、5(2)、3(1)、2(4)、4(2)、1(4),并且成本 = 13
for 循环的外观有何建议?

【问题讨论】:

标签: c# algorithm recursion


【解决方案1】:

Travelling Saleman Problem 是一个优化问题,已知为NP-hard,这意味着它不太可能承认有效的优化算法。话虽如此,可以使用Dynamic Programming 在指数运行时间范围内解决它。可以在here 或(在最近的出版物中)here 找到这样的算法。

【讨论】:

  • 我明白了 TSP 的概念,但所有这些公式对我来说都太难理解了:/
【解决方案2】:

有 3 个主要功能。第一个和第二个创建所有可能的道路,第三个计算它们的成本。希望对你有帮助

static void AllRoads(int[] a, int size, int n, RoadContainer VisiKeliai, int[][]map)
        {
            // if size becomes 1 then prints the obtained 
            // permutation 
            if (size == 1)
                printArr(a, n, VisiKeliai,map);

            for (int i = 0; i < size; i++)
            {
                AllRoads(a, size - 1, n, VisiKeliai,map);

                // if size is odd, swap first and last 
                // element 
                if (size % 2 == 1)
                {
                    int temp = a[0];
                    a[0] = a[size - 1];
                    a[size - 1] = temp;
                }

                // If size is even, swap ith and last 
                // element 
                else
                {
                    int temp = a[i];
                    a[i] = a[size - 1];
                    a[size - 1] = temp;
                }
            }
        }

        static void printArr(int[] a, int n, RoadContainer VisiKeliai, int[][]map)
        {
            string s = "0";
            // Console.WriteLine("dassaddsasdaasdas"  + a.Length);
            for (int i = 0; i < n; i++)
            {
                // Console.Write(a[i] + " ");
                s += Convert.ToString(a[i]);
            }
            s = s.Insert(s.Length, "0");
            Road r = new Road(s);
            CalculatingPrice(r, map);
            VisiKeliai.AddRoad(r);
            //Console.WriteLine(s);
        }

        public static void CalculatingPrice(Road Kelias, int[][] map)
        {           
            //  021430 Road im checking
            Console.WriteLine("Kelias: " + Kelias.Path);
            for (int i = 0; i < Kelias.Path.Length - 1; i++)
            {
                int nextI = i + 1;
                for (int j = 0; j < 5; j++)
                {
                    for (int m = 0; m < 5; m++)
                    {
                        string a = Convert.ToString(m);
                        string aaa = Convert.ToString(Kelias.Path[i]);
                        string b = Convert.ToString(j);
                        string bbb = Convert.ToString(Kelias.Path[nextI]);
                        if (a == aaa && b == bbb)
                        {
                            Console.WriteLine(a + "--" + aaa + "---" + b + "----" + bbb + "-------------" + map[j][m]);
                            Kelias.Cost += map[j][m];
                        }

                    }

                }
            }

        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-11
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-20
    • 2021-02-06
    相关资源
    最近更新 更多