简体   繁体   English

Q矩阵单元之间的最短路径

[英]Shortest Path Between Q Matrix Cells

I saw this problem in a local competition and I am trying to solve it, 我在当地比赛中看到了这个问题,我正在努力解决,

I am given a matrix with ' r ' rows and ' c ' columns. 我得到了一个带有“ r ”行和“ c ”列的矩阵。 Then I am given ' Q ' cells, 然后给我' Q '单元格,

My task is to find the shortest path: Starting at the top left corner ' (0,0) ' , ending at the bottom right corner ' (row-1,column-1) ' and travessing all ' Q ' cells . 我的任务是找到最短的路径:从左上角' (0,0) '开始,在右下角' (row-1,column-1) '结束并遍历所有' Q '单元。 I can move either right, left, up and down. 我可以左右,左右移动。

Brute Force is out of option (has I only have 1 second of CPU time and I can be given at most 50 columns and rows). 蛮力是不可能的(我只有1秒的CPU时间,最多只能给我50列和行)。

Here is a Sample Input: 这是一个示例输入:

6 3   
3        
0 1    
4 2  
5 2 

Here is the Sample Output: 这是示例输出:

7

I am thinking about some sort of BFS, but I have no idea how to apply it to this specific problem. 我正在考虑某种BFS,但我不知道如何将其应用于此特定问题。

Any help or advise would be much appreciated ! 任何帮助或建议将不胜感激!

Calculate the shortest path between every Q and starting and finishing point. 计算每个Q与起点和终点之间的最短路径。 Create a graph with these distances. 创建具有这些距离的图形。

Since you can only move orthogonally, the shortest distance between two points is the sum of the absolute differences between their co-ordinates. 由于只能正交移动,因此两个点之间的最短距离是其坐标之间的绝对差之和。

Select an algorithm to visit all nodes in your distance graph. 选择一种算法来访问距离图中的所有节点。 Possible starting point Bottom line is that this has become the "travelling salesman problem" and you will need to decide which of the many available algorithms suits you best. 可能的出发点底线是,这已成为“旅行推销员问题”,您将需要确定许多可用算法中最适合您的算法。

If I am understanding your input, the distance graph looks like this 如果我理解您的输入,则距离图如下所示

Locations

B  0 0
Q1 0 1    
Q2 4 2  
Q3 5 2 
E  3 3

Distances

B  0  1  6  7  6
Q1 0  0  5  6  5
Q2 0  0  0  1  2
Q3 0  0  0  0  3
E  0  0  0  0  0

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

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