墙与门的算法(广度优先搜索)

func wallsAndGates(_ rooms: inout [[Int]]) {
    if rooms.isEmpty || rooms.first!.isEmpty {
        return
    }
    let INF = 2147483647
    let m = rooms.count, n = rooms.first!.count
    
    // 光环找门,直到找到门或者没有元素为止
    func findValue(_ rooms: [[Int]], _ i: Int, _ j: Int, _ step: inout Int) {
        var set: [String] = []
        var quene: [String] = []
        quene.append("\(i)+\(j)")
        while !quene.isEmpty {
            step += 1
            // 广度优先搜索的核心层循环
            for _ in 0..= 0 {
                    quene.append("\(x)+\(y-1)")
                }
                if !set.contains("\(x-1)+\(y)") && x-1 >= 0 {
                    quene.append("\(x-1)+\(y)")
                }
            }
        }
        step = INF
    }
    
    // 对每个非墙与门的元素做光环找门
    for i in 0..

你可能感兴趣的:(广度优先搜索,算法)