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

时间:2018-02-09 10:56:15

标签: ios swift matrix

我想找到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()!)
    }
}

我坚持计算最小值并且无法强制执行相邻指数,如水平或对角线移动。任何帮助将不胜感激。

enter image description here

3 个答案:

答案 0 :(得分:3)

您可以使用Dijkstra算法,这里是Swift implementation

答案 1 :(得分:1)

如前所述,使用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)

此解决方案找到从顶层角落到右下角的最短路径,但您可以通过更改函数内的起始条件和比较操作来轻松调整它。

答案 2 :(得分:0)

Ref-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))
相关问题