gliyao
1/29/2015 - 2:11 AM

Pyramid

Pyramid


var map = [[Int]]()
var path = [[Int]]()

var level = 3
var stack = [Int]()

func nextSetp(currentStep: Int, currentIndex: Int) {
    
    let value = map[currentStep][currentIndex]
    stack.append(value)
    
    if currentStep < level - 1 {
        nextSetp(currentStep + 1, currentIndex)
        nextSetp(currentStep + 1, currentIndex + 1)
    }
    else {

        var newPath = stack
        path.append(newPath)
        println(newPath)
    }
    stack.removeLast()
}

func pyramid(level: Int) {
    
    // make map
    for i in 0..<level {
        var arr = [Int]()
        for j in 0..<i+1 {
            arr.append(j)
        }
        map.append(arr)
    }
    
    nextSetp(0, 0)
    
    var pathValues: [Int] = [Int]()
    for value in path {
       pathValues.append(value.reduce(0,+))
    }
    
    println(pathValues)
    pathValues.sort { $0 < $1 }
    println(pathValues)
}