Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 以最低的成本从左上到右下移动棋盘_Algorithm - Fatal编程技术网

Algorithm 以最低的成本从左上到右下移动棋盘

Algorithm 以最低的成本从左上到右下移动棋盘,algorithm,Algorithm,有一个棋盘,有m行和n列。在每个检查器中,有一个整数表示通过该检查器的成本。现在我应该找到一个从左上角棋盘格到右下角棋盘格的成本最低的路径 输入 在第一行中有两个整数m和n,表示行数和列数。在下面的m行中,每行有n个整数,表示通过每个检查器的成本 输出 包含表示最低成本的整数的单行 样本输入 4 5 1 100 1 1 1 1 100 1 100 1 1 1 1 100 1 100 100 100 100 1 12 样本输出 4 5 1 100 1

有一个棋盘,有m行和n列。在每个检查器中,有一个整数表示通过该检查器的成本。现在我应该找到一个从左上角棋盘格到右下角棋盘格的成本最低的路径

输入
在第一行中有两个整数m和n,表示行数和列数。在下面的m行中,每行有n个整数,表示通过每个检查器的成本

输出
包含表示最低成本的整数的单行

样本输入

4 5
 1  100  1   1  1
 1  100  1  100 1
 1   1   1  100 1
100 100 100 100 1
12
样本输出

4 5
 1  100  1   1  1
 1  100  1  100 1
 1   1   1  100 1
100 100 100 100 1
12

我试图用动态规划来解决这个问题。如果每一步只能向下或向右走,那就很容易了。但我不知道如何才能从四面八方找到一个检查器。谁能告诉我应该使用什么策略吗?

如果成本不能为负,这是一个非常简单的问题:

  • 创建一个图表。节点表示检查器。如果两个棋盘格相邻,则存在一条边

  • 将每个棋盘格的成本指定给进入棋盘格对应节点的所有边的成本

  • 使用标准查找从开始节点到目标的最短路径,时间复杂度为


  • 否则,如果成本可以是任何整数,则会有点复杂。您可能希望将第三步替换为解决此问题。那么最糟糕的时间复杂度是。

    如果每个成本
    >=0

    create an 'output' array the same size as the 'cost' array
    set all entries in the 'output' array to 'unknown'
    the first entry in the 'output' array equals the first entry of the 'cost' array
    write the coordinates of the first entry {0,0} to a queue
    while the queue is not empty
    {
        read a set of coordinates from the queue
        for each neighboring square
        {
            compute the cost to move from the current square to the neighbor
            if ( the current cost of the neighbor is unknown or higher )
            { 
                set the cost of the neighboring square to the new lower cost
                add the coordinates of the neighboring square to the queue
            }
        }
    }
    
    一旦队列为空,输出数组将以最低的成本到达每个方块。下图显示了该算法如何处理问题中给出的成本数组。绿色方块是在算法的每个过程中修改的方块


    你能解释一下结果是如何得到12的吗?你能有负成本吗?@smn\u onrocks你可以选择所有的1(开始、下降、下降、右、右、右、上、上、右、右、右、右、右、下、下)@Amir所有成本都是正整数。我担心这个算法会导致超过时间限制。无论如何,我会试试看。如果只是ICPC层面的问题,ThxIt就不会。