解决流行算法路径遍历类型的方法

Approach used in solving Popular Algorithmic Path Traversal Type

我能想到的是,如果我们从最后一行开始,然后向上移动,那么我们必须寻找所有可能的时间复杂度 O(2^n) 的组合,但是还有另一个解决此类问题的有效方法?

这是 shortest path problem 的变体,您的图表是:

G = (V,E)
V = { (x,y) | for each cell in the matrix } U { (-1,-1) } //assuming 0 based matrix
E = { ((x1,y),(x2,y+1)) | x1 < x2 } // or x1<=x2 - depends if you can go straight down
w((x1,y1),(x2,y2)) = value(x2,y2) //weight function

现在,这是一个图表(实际上是偶数 Directed Acyclic Graph - DAG) and you can apply a shortest path algorithm on it from a single source (-1,-1) to multiple targets of the format (x,n-1) - where n is the total number of rows. One algorithm to solve it is Dijkstra's Algorithm


理解这是一个DAG后,我们甚至可以用直接的DP解决方案进一步优化它(实际上模仿了最短路径算法,但利用了DAG特性):

D(-1,-1) = 0
D(x,y) = min { D(x', y-1) | for each x'>x }  U {infinity} + value(x,y)

最终答案将是min{ D(x,n-1) | for all values of x}

此解决方案在 O(n*m*m) 中运行,其中 n 是行数,m 是列数。