简体   繁体   English

在2D阵列中找到最长的路线

[英]Find longest route in 2D array

So I got this assignment which I am not sure how to aprroach. 所以我得到了这项任务,我不知道该怎么做。 I got a 2D array in which some cells are forbidden to visit. 我得到了一个2D阵列,禁止访问某些单元格。 Then I need to traverse the whole array choosing the longest route without entering the forbidden cells. 然后我需要遍历整个阵列选择最长的路线而不进入禁用的单元格。 I can also not "go back" and take another turn, the traversal should always go "forward". 我也不能“回去”再转一圈,遍历应该总是“向前”。 The output should be the number of cells visited and in the correct order. 输出应该是访问的单元格数和正确的顺序。 The algorithm should be farily scalable, at least for an array with 100x100 cells. 算法应该是可扩展的,至少对于具有100x100个单元的阵列。 Below is a picture showing the task. 下面是一张显示任务的图片。

在此输入图像描述

As in the picture above, there is really just two solutions: Either follow along the edge and then go down one cell and then up again, just like the example. 如上图所示,实际上只有两种解决方案:沿着边缘然后沿着一个单元格向下然后再向上,就像示例一样。 Or it could just have followed along the complete edge. 或者它可能只是沿着完整的边缘。 The number to cells visited should still be the same; 访问过的单元格的数量应该保持不变; 12. 12。

Now I have searched alot on this and found many variations and to my understanding, I should be able to use maybe Djikstras, Bellman-Ford, A* algorithms or some kind of Depth/Breadth-first graph traversal. 现在我已经搜索了很多,发现了很多变化,据我所知,我应该可以使用Djikstras,Bellman-Ford,A *算法或某种Depth / Breadth-first图遍历。 But I am totaly stuck and not sure where to go from here. 但我完全陷入困境,不知道从哪里开始。

This problem is NP-hard, as shown in A. Itai, C. Papadimitriou, J. Szwarcfiter, Hamiltonian paths in grid graphs . 这个问题是NP难的,如A. Itai,C.Papadimitriou,J。Szwarcfiter,网格图中的哈密顿路径所示 So no exact polynomial time algorithm exist. 因此,不存在精确的多项式时间算法。

In practice, the problem of you size should be solvable by modern Constraint Solvers . 在实践中,现代Constraint Solvers可以解决您的大小问题。 The review of the methods how to make Constraint Problem for the longest path search could be found at Q. Pham, Y. Deville, Solving the Longest Simple Path Problem with Constraint-Based Techniques . 关于如何为最长路径搜索制定约束问题的方法的回顾可以在Q. Pham,Y.Deville找到, 用基于约束的技术解决最长的简单路径问题

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

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