繁体   English   中英

如何使用 Swift 在二维矩阵中找到成本最低的路径

[英]How to find the path of lowest cost in a 2D Matrix using Swift

我想在 2D 矩阵中找到成本最低的路径,这里有一些与此相关的图像:

给定一个 2D 矩阵,我想沿着三个允许的路径遍历:1) 水平,2) 右上对角或 3) 右下对角。

这是我迄今为止找到最低成本的代码:

var input2 = [ [2,3,4], [2,6,8], [3,4,6] ]

func calculate() {
    var sums = [Int]()
    var tmp = [Int]()
    for o in input2 {
        for i in o {
            tmp.append(i)
        }
        sums.append(tmp.min()!)
    }
}

我被困在计算最小值的这一点上,无法强制执行相邻的指数,如水平或对角线运动。 任何帮助将不胜感激。

在此处输入图片说明

您可以使用 Dijkstra 算法,这是一个Swift 实现

如前所述,使用 Dijkstra 算法找到最短路径。 这是我想出的一个未经优化的快速解决方案。

let M = 3
let N = 3
var input = [[2,3,4],
             [2,6,8],
             [3,4,6]]
var shortestPaths = [[Int]](repeatElement([Int](repeatElement(Int.max, count: N)), count: M))

func search(_ currentPosition: (Int, Int),
                 _ path: [(Int, Int)],
                 _ totalCost: Int,
                 _ shortestPath: [(Int, Int)],
                 _ lowestCost: Int)
    -> ([(Int, Int)], Int) {
        if (totalCost >= lowestCost) {
            return (shortestPath, lowestCost)
        }
        let (i, j) = currentPosition
        var lowestCost = lowestCost
        var shortestPath = shortestPath
        if (currentPosition == (M - 1, N - 1)) {
            return (path, totalCost)
        }
        if (shortestPaths[i][j] < totalCost) {
            return (shortestPath, lowestCost)
        }
        shortestPaths[i][j] = totalCost
        if (i > 0) {
            if (j > 0) {
                let result = search((i - 1, j - 1), path + [(i - 1, j - 1)], totalCost + input[i - 1][j - 1], shortestPath, lowestCost)
                if (result.1 < lowestCost) {
                    lowestCost = result.1
                    shortestPath = result.0
                }
            }
            if (j < N - 1) {
                let result = search((i - 1, j + 1), path + [(i - 1, j + 1)], totalCost + input[i - 1][j + 1], shortestPath, lowestCost)
                if (result.1 < lowestCost) {
                    lowestCost = result.1
                    shortestPath = result.0
                }
            }
            let result = search((i - 1, j), path + [(i - 1, j)], totalCost + input[i - 1][j], shortestPath, lowestCost)
            if (result.1 < lowestCost) {
                lowestCost = result.1
                shortestPath = result.0
            }
        }
        if (i < M - 1) {
            if (j > 0) {
                let result = search((i + 1, j - 1), path + [(i + 1, j - 1)], totalCost + input[i + 1][j - 1], shortestPath, lowestCost)
                if (result.1 < lowestCost) {
                    lowestCost = result.1
                    shortestPath = result.0
                }
            }
            if (j < N - 1) {
                let result = search((i + 1, j + 1), path + [(i + 1, j + 1)], totalCost + input[i + 1][j + 1], shortestPath, lowestCost)
                if (result.1 < lowestCost) {
                    lowestCost = result.1
                    shortestPath = result.0
                }
            }
            let result = search((i + 1, j), path + [(i + 1, j)], totalCost + input[i + 1][j], shortestPath, lowestCost)
            if (result.1 < lowestCost) {
                lowestCost = result.1
                shortestPath = result.0
            }
        }
        if (j > 0) {
            let result = search((i, j - 1), path + [(i, j - 1)], totalCost + input[i][j - 1], shortestPath, lowestCost)
            if (result.1 < lowestCost) {
                lowestCost = result.1
                shortestPath = result.0
            }
        }
        if (j < N - 1) {
            let result = search((i, j + 1), path + [(i, j + 1)], totalCost + input[i][j + 1], shortestPath, lowestCost)
            if (result.1 < lowestCost) {
                lowestCost = result.1
                shortestPath = result.0
            }
        }
        return (shortestPath, lowestCost)
}

let shortPath = search((0, 0), [(0, 0)], input[0][0], [], Int.max)
print(shortPath)

//output:
//([(0, 0), (1, 1), (2, 2)], 14)

这个方案是寻找从顶层角到右下角的最短路径,但是你可以通过改变启动条件和函数内部的比较操作来轻松调整它。

参考 - https://www.geeksforgeeks.org/min-cost-path-dp-6/

class Solution {
    
    var input: [[Int]] = [[Int]]()
    
    func findMinCostPath(input: [[Int]], row: Int, column: Int) -> Int {
        self.input = input
        return minCost(row, column)
    }
    
    func minCost(_ row: Int, _ column: Int) -> Int {

        guard row >= 0 && column >= 0
        else {
            return Int.max
        }

        if row == 0 && column == 0 {
            return input[0][0]
        } else {
            let minCosts = min(minCost(row - 1, column - 1),
                               minCost(row - 1, column),
                               minCost(row, column - 1))
            if minCosts == Int.max {
                return minCosts
            } else {
                return input[row][column] + minCosts
            }
        }
    }
}

let input = [[1, 2, 3], [4, 8 , 2], [1, 5, 3]]
print(Solution().findMinCostPath(input: input, row: 2, column: 2))

暂无
暂无

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

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