Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.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_Graph_Dynamic Programming_Greedy - Fatal编程技术网

Algorithm 找到这样一种方式,即在途中跳跃的最大距离可能是最小的

Algorithm 找到这样一种方式,即在途中跳跃的最大距离可能是最小的,algorithm,graph,dynamic-programming,greedy,Algorithm,Graph,Dynamic Programming,Greedy,有一个平台,可以放置在不同的高度。例如,这些地图显示了平台是如何放置的(在程序中,它表示为矩阵NxM、|N |、|M |,以解析地图并查找节点: for i from 1 to N for j from 1 to M if map(i, j) == 'S' nodes.add(i, j); start = nodes.Count; elseif map(i, j) == 'D' n

有一个平台,可以放置在不同的高度。例如,这些地图显示了平台是如何放置的(在程序中,它表示为矩阵
NxM、|N |、|M |,以解析地图并查找节点:

for i from 1 to N
    for j from 1 to M
        if map(i, j) == 'S' 
            nodes.add(i, j);
            start = nodes.Count;
        elseif map(i, j) == 'D' 
            nodes.add(i, j);
            dest = nodes.Count;
        elseif map(i, j) == '_'
            nodes.add(i, j);
        end
    end
end
在上面的伪代码中,我假设
节点。添加(I,j)
节点.x=1
节点.y=j
添加到节点列表中

然后,要构造邻接矩阵:

n = nodes.Count;
adj = n by n matrix, filled with +inf;
for i from 1 to n
    for j from i + 1 to n
       if (nodes[i].x == nodes[j].x) || (nodes[i].y == nodes[j].y)
           adj(i, j) = abs(nodes[i].x - nodes[j].x) +
               abs(nodes[i].y - nodes[j].y);
       end
    end
end

剩下的是一个。用于查找
start
dest
节点之间的最短路径。

多亏了上面的帖子,我决定完成这个想法,得到了这个代码,对于我收到的测试用例,它工作得很好。因此,这个想法是:

  • 根据给定的平台地图,有必要创建一个图,其中一个节点表示一个平台(包括起始平台和目标平台),节点之间的边表示为它们之间的距离
  • 当你形成这个图时,你的目标是找到最小生成树,并在这个树中找到边的最大权重——这就是答案。 代码非常大,请在我的github上查看!注意
    1
    表示平台,
    2
    表示开始,
    3
    表示目的地:

  • 我不需要找到最短路径!我需要找到包含最小边的路径!@Ayratarifull当然,但算法是相同的。在增加路径和更新当前最短路径时,您不需要比较
    求和(权重)
    ,而是
    最大(权重)
    @AyratArifullin除此之外,如果有多条路径具有相同的最长跳跃值,该怎么办?您不想返回最短的路径吗?或者返回跳跃次数最少且最大长度的路径。所有这些都使用相同的算法,但比较两条路径的方法不同。@AyratArifullin请记住,Dijkstra的方法实际上是一种选择最大化算法,并试图最小化“成本”,而“长度”仅来自著名问题“最短路径问题”的术语。现在,当问题中的“成本”是“最长跳跃”时,只需在相同的算法中使用您自己的成本。我几乎没有更正您的建议并提出解决方案。您可以在下面查看。感谢您支持我的想法!您刚刚进一步推动了我
          _ _ _    
        D _   _ _  
        +       _ _
        +         _
        S _ _ _ _ _
    
    for i from 1 to N
        for j from 1 to M
            if map(i, j) == 'S' 
                nodes.add(i, j);
                start = nodes.Count;
            elseif map(i, j) == 'D' 
                nodes.add(i, j);
                dest = nodes.Count;
            elseif map(i, j) == '_'
                nodes.add(i, j);
            end
        end
    end
    
    n = nodes.Count;
    adj = n by n matrix, filled with +inf;
    for i from 1 to n
        for j from i + 1 to n
           if (nodes[i].x == nodes[j].x) || (nodes[i].y == nodes[j].y)
               adj(i, j) = abs(nodes[i].x - nodes[j].x) +
                   abs(nodes[i].y - nodes[j].y);
           end
        end
    end