在二维数组中查找路径

Finding paths in a 2d array

我有一个二维整数数组,想找到从左列到右列的和最小的路径,当可以向上、向下和向左时。我从所有行的 loop 开始,并尝试将路径构建为列表。

(defparameter *test-array*
  #2A((131 673 234 103 18)
      (201 96 342 965 150)
      (630 803 746 422 111)
      (537 699 497 121 956)
      (805 732 524 37 331)))

(defun find-paths (array &aux (height (array-dimension array 0)) (width (array-dimension array 1)))
  "Returns the possible paths across a given 2d array from the left column to
  the right column."
  (loop :for i :from 0 :below height
        ;; We have three ways per starting point up, left and down.
        ;; In the first and last row there are two ways, only.
        :append (loop :for (j l) :in '((1 0) (0 1) (-1 0))
                       :when (and (>= (+ j i) 0) (< (+ j i) height))
                         :collect (list (aref array i 0) (aref array (+ i j) l))) :into paths
        :finally (return paths)))

我想知道这是否真的能带来好的解决方案。恐怕它会变得越来越复杂,并且会消耗内存,以将整个数组转换为代表所有可能路径的列表列表。据我了解,它基本上是一个图表。但是我怎样才能在不浪费内存的情况下从数组创建图形呢?

这似乎可以通过动态规划解决。

我将创建一个具有相同维度的新数组,它在每个坐标处保存两个值:到达它的最佳路径及其成本。在最左边的列初始化这些值,然后将它们一直传播到右边。

然后,向上传播每个元素路径,直到它是一个改进。然后离开,然后下来。循环查看说明,直到 none 个说明有所改善。

最后,最右列的最佳路径就是解决方案。